if, else if, else 语句:在代码块中

if, else if, else statements: Within the blocks of code

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

以下是关于 a、b 和 c 必须执行的操作的具体说明: 一)驱动: 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
        }
    }

}

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

第一个:

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 教程和 class 信息,例如: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html 还有这个: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html

我已尝试准确了解您需要根据 post 执行的操作。我建议在您的代码中进行以下更正:

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

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

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

    这导致循环直到用户 select c 然后 2.

  3. 我建议您将 scan.next() 更改为 scan.nextLine() 以过滤掉用户的不正确输入,这些输入将由 else 条件处理 if - 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. 您需要将行 litresUsed + litresAdded = gasTotal 更正为 startingLiter = startingLitre - litresUsed;,因为您必须跟踪所有使用的气体量和填充的气体量。该语句可以写成 startingLiter -= litresUsed; 两者是等价的。

  8. 您需要更正您的数学方程式 litresUsed + litresAdded = gasTotal;,这对编程语句 startingLitre = startingLitre + litresAdded; 没有任何意义,因为您基本上是在添加坦克.

  9. 您必须为 c 输入编写新的 else if 条件。它需要第二个输入 12,如您原来的 post 中所述。并且基于第二个输入 if 1 然后它打印状态,即 KM 运行 和 Liter in tank。 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;
    }
    

谢谢大家的帮助,我能够清理代码并删除所有不必要的东西。它就是这样结束的,现在它完美地工作了(以防万一有人偶然发现这个问题并想看到最终的工作产品)!我肯定从中学到了很多东西。

 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)
        }
    }
    }

}