【问题标题】:if, else if, else statements: Within the blocks of codeif、else if、else 语句:在代码块内
【发布时间】:2015-04-30 04:25:17
【问题描述】:

我是一名初级程序员,目前正在为这项任务撞墙。 我必须创建一个程序来“模拟”汽车的使用,所以它是:开车、停车和加油。它所做的第一件事是询问您的油箱可以容纳多少升汽油,以及目前油箱中有多少升。然后,它会询问用户是否想要 a) 开车、b) 加油或 c) 停车。我需要获取用户输入(我已经知道该怎么做),并且根据用户输入的是 a、b 还是 c,它会转到特定的代码块并运行它。

以下是关于 a、b 和 c 必须做什么的具体说明: a) 驱动器: 1.输入行驶公里数(用户输入这个) 2. 输出使用了多少升的气体,以及罐中剩余的气体量。 (我们假设汽车平均使用 0.1 L/Km)

b) 填满 1. 用户必须输入他们希望添加的升数 2、罐内输出升数。 (用户输入的升数不能超过罐子的容量)

c) 公园 1.输出油箱升数,总行驶公里数。 2. 退出循环

我是否使用了正确的循环?如何让代码运行方程(查看红色下划线)?请帮帮我,我迷路了。

 public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("How many litres of gas can your tank hold?");
        int litreCap = scan.nextInt();
        System.out.println("How much gas is currently in your tank?");
        int startingLitre = scan.nextInt();
        System.out.println("Ok! Do you want to \n a) Drive \n b) Fill up, or \n c) Park");
        String abc = scan.next();
        String a, b, c;
        a = String.valueOf(1); //Wasn't sure if the problem had to do with the fact
        b = String.valueOf(2); //That I hadn't identified a,b and c
        c = String.valueOf(3);
        double litresUsed, gasTotal;

        if (abc .equals(a)) { //this is the drive section
            System.out.println("Please enter the amount of Kilometers driven.");
            int KmCount = scan.nextInt();
            litresUsed = 0.1*KmCount;//underlined yellow
            startingLitre - litresUsed = gasTotal;//this is underlined red
          }
        else if (abc .equals(b)){ //this is the fill up section
            System.out.println("OK! Please enter how many litres you are going to add.");
            int litresAdded = scan.nextInt(); //this is underlined yellow
            litresUsed + litresAdded = gasTotal; //underlined red
        }
        else {
            //enter c) Park code here
        }
    }

}

【问题讨论】:

  • 我在您的代码中没有看到任何循环?如果 a,b,c 是字符串,那么 abc 是什么?我从来没有见过你可以像这样连接字符串?!!! 你甚至可以运行你的代码吗?
  • 让我建议您为变量添加更多描述性名称,例如,不要使用 a,而是使用类似 driveSelected 的名称。可以帮助您(以及您之后的其他人)记住 (a) 选项是用户想要驾驶的东西。
  • something else: startingLitre - litresUsed = gasTotal; 请记住,当您将某些内容分配给变量时,目标变量位于左侧,源(表达式、其他变量、执行的方法)位于右侧声明,像这样:gasTotal = startingLitre - litresUsed;
  • 我添加了我的最后一条评论作为答案,因为这会让你的代码运行一个方程式。顺便说一句,@KickButtowski 是对的,您的代码中没有循环。
  • @germanio 你的回答看起来像是一个评论,只是友好的观察;)

标签: java loops if-statement netbeans switch-statement


【解决方案1】:

根据您的帖子,我已尝试准确了解您需要做什么。我建议在您的代码中进行以下更正:

  1. 您需要最后打印运行的公里数。所以你必须引入一个新的int 变量比如runKm 并在你开始循环之前初始化为0:

    int runKM = 0;
    
  2. 您在帖子中声明程序应该循环直到用户想要退出循环,首先选择“c”,然后选择2。所以为了有一个循环,你必须引入一个while循环,其中一个新的boolean变量说continued初始化为true并启动一个while循环,变量continued作为它的检查条件最后在所有 if - else 块之后关闭它像这样:

    boolean continued = true;
    while(continued){
    //The checking conditions are implemented
    }
    

    这会导致循环直到用户选择c,然后选择2

  3. 我建议您将 scan.next() 更改为 scan.nextLine() 以过滤掉用户的错误输入,这些输入将由 if - else 块的 else 条件处理。

  4. String a, b, c 因为它们不是必需的,所以将它们连同它们在程序中的引用一起删除。

  5. 现在您必须更改 ifelse if 的条件以将输入直接与所需的 String 值进行比较,这样您就不需要变量 a, b, c。像这样:

    abc.equals("a") //instead of abc.equals(a)
    abc.equals("b") //instead of abc.equals(b)
    
  6. if-else 块的a 条件下,您需要添加一个新行runKM += KmCount; 以跟踪所有运行的公里数。这也可以写成runKM = runKM + KmCount;两者是等价的。

  7. 1234563填充的气体量。这个语句可以写成startingLiter -= litresUsed;两者是等价的。
  8. 您需要将没有任何意义的数学方程式 litresUsed + litresAdded = gasTotal; 更正为编程语句 startingLitre = startingLitre + litresAdded;,因为您基本上是在添加水箱中剩余的数量。

    李>
  9. 您必须为c 输入编写新的else if 条件。如原始帖子中所述,它需要12 的第二个输入。如果1 则基于第二个输入,它会打印状态,即KM 运行和油箱升。和2 或任何其他int 它将continued 变量更改为false,从而导致while loop 退出。像这样:

    else if(abc.equals("c")) {
        System.out.println("OK! Parked!!! What do you want to do?\n 1)Check your car status\n 2) exit ")
        int response = scan.nextInt();
        if(response == 1){
           System.out.println("Number of KM run = "+runKM +"\nAmount of litre in tank = "+startingLitre);
        }else {
           continued = false;
        }
    }
    
  10. 最后你必须设置一个else 条件,它不满足用户输入的abc。并为用户写一条适当的消息,然后像这样再次循环。

    else{
       System.out.println("Wrong value entered. Please try again");
       continue;
    }
    

【讨论】:

  • 您的帖子已被标记为低质量答案。请提供一些解释。
  • @KickButtowski 我希望现在可以了
  • 我没有投票给你,但我会尝试研究你的答案并投票给你。
  • @KickButtowski 我知道这是在喂汤,但是你如何回答一个不知道编程语句和数学方程之间区别的人。
  • 只需给出提示代码示例并尽可能多地解释。你知道其他人也会看到这篇文章。我同意你所说的,但摆脱你的代码并解释。我相信其他人会看到你的帖子,你会有更好的结果
【解决方案2】:

因此,您共享的代码似乎存在一些小问题。

第一:

String abc = scan.next();
String a, b, c;
a = String.valueOf(1);
b = String.valueOf(2);
c = String.valueOf(3);

如果您想从命令行读取用户选项,您可能已经在abc 变量中拥有它。只需使用 if/if-else/else 块来询问该变量的值。

第二:

startingLitre - litresUsed = gasTotal;

当你给一个变量赋值时,目标变量在左边,源变量(一个表达式、方程、其他变量,甚至是一个方法调用)在左边语句的右侧,如下所示:

gasTotal = startingLitre - litresUsed;

希望这会有所帮助。 您可以随时查看 Oracle 教程和课程信息,如下所示: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html 和这个: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html

【讨论】:

  • OP 应该解释她自己的代码,对我来说,abc 似乎是为了阅读用户输入。但重点是方程式的运行方式,我的回答对此进行了解释。
  • @KickButtowski:那段代码有一些错误,我同意,但我不会为他/她解决所有问题,练习的目的是练习和学习,对吧?
  • 我完全同意。但规则是要么有完整的答案,要么不显示任何内容。你可以标记这篇文章,但你的答案并不完整,也没有解决操作所面临的所有主要问题。
  • 听起来不错,但知道不鼓励给出代码答案,祝你好运
【解决方案3】:

感谢大家的帮助,我能够清理代码并带走所有不必要的东西。这就是它的结束方式,现在它可以完美运行(以防有人偶然发现这个问题并希望看到最终的工作产品)!我确实从中学到了很多。

 public class CarGame {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("How many litres of gas can your tank hold?");
        int litreCap = scan.nextInt();

        System.out.println("How much gas is currently in your tank?");
        int startingLitre = scan.nextInt();

        System.out.println("Ok! Do you want to \n a) Drive \n b) Fill up, or \n c) Park");
        String abc = scan.next();

        int litresUsed;
        int runKM = 0; //Kilometer counter (N.M 2015)

        boolean continued = true;//Keeps looping a, b, and c until user either parks or errs (N.M 2015)
        while(continued){

        if (abc .equals("a")) { //Drive section (N.M 2015)
            System.out.println("Please enter the amount of Kilometers driven.");
            int KmCount = scan.nextInt();
            litresUsed = (int) (0.1*KmCount);
            startingLitre = startingLitre - litresUsed;
            runKM = runKM + KmCount;
                if (startingLitre <= 0) { //this is the error
                    System.err.println("Uh-oh! You ran out of gas! Try again.");
                break; //If they run out of gas, the program ends like a game over (N.M 2015)
                }
            System.out.println("You used " + litresUsed + " litre(s) of gas. That leaves you with \n" + startingLitre + " litres of gas.");
            System.out.println("You have driven for " + runKM + " kilometers.");

            System.out.println("What do you want to do now? \n a) Drive \n b) Fill up, or \n c) Park");
            abc = scan.next();
            continued = true;
          }
        else if (abc .equals("b")){ //Fill up section (N.M 2015)
            System.out.println("OK! Please enter how many litres you are going to add.");
            int litresAdded = scan.nextInt();
            startingLitre = startingLitre + litresAdded;
                if (startingLitre > litreCap){ //this is the error
                System.err.println("Hey! That is too much gas! Your tank can only hold " + litreCap + " litres of gas!");
                break;
                }
            System.out.println("You added " + litresAdded + " litres of gas. Now, your car has " + startingLitre + " litres of gas.");

            System.out.println("What would you like to do now? \n a) Drive \n b) Fill up, or \n c) Park");
            abc = scan.next();
            continued = true;
        }
        else if (abc .equals("c")) { //Park section (N.M 2015)
            System.out.println("You have now parked!");
            System.out.println("Number of Km run: " + runKM + "\nAmount of litres in tank: " + startingLitre);
            continued = false; //ends program (N.M 2015)
        }
        else {
            continued = false; //ends program (N.M 2015)
        }
    }
    }

}

【讨论】:

    猜你喜欢
    • 2019-04-09
    • 1970-01-01
    • 2018-03-29
    • 2014-11-11
    • 2015-05-16
    • 2017-02-05
    • 1970-01-01
    相关资源
    最近更新 更多