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

时间:2015-04-30 04:25:18

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

我是一名初学程序员,目前正在为此任务敲打我的头。 我必须创建一个“模拟”汽车使用的程序,因此它可以:驾驶,停车和填充。它首先要问的是你的油箱可容纳多少升气体,以及目前油箱中有多少升。然后,它询问用户是否想要a)驾驶,b)填满,或c)停车。我需要获取一个用户输入(我已经知道该怎么做),并且根据用户是否输入a,b或c,它会转到这个特定的代码块并运行它。

以下是a,b和c必须执行的具体说明: a)驱动器: 1.输入驱动的Km量(用户输入此值) 2.输出使用了多少升气体,以及储罐中剩余的气体量。 (我们假设汽车平均使用0.1 L / Km)

b)填写 1.用户必须输入他们想要添加的升数 2.输出罐中的升数。 (用户不能输入比坦克更多的升数)

c)Park 1.输出罐中的升数和Km驱动的总数。 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
        }
    }

}

3 个答案:

答案 0 :(得分:3)

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

  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()以过滤掉用户的错误输入,这些输入将由else块的if - else条件处理

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

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

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

  7. 您需要更正行runKM = runKM + KmCount;,这基本上是数学中的等式,而不是编程中的赋值运算符,litresUsed + litresAdded = gasTotal,因为您必须跟踪所有金额使用的气体和充满的气体量。这句话本可以写成startingLiter = startingLitre - litresUsed;两者都是等价的。

  8. 您需要更正您的数学等式startingLiter -= litresUsed;,这对编程语句litresUsed + litresAdded = gasTotal;没有任何意义,因为您基本上会增加您在坦克中留下的金额。

  9. 您必须为startingLitre = startingLitre + litresAdded;输入编写新的else if条件。它需要您原始帖子中所述的c1的第二个输入。并且基于第二个输入if 2然后它打印状态,即KM run和Liter in tank。和1或任何其他2它会将int变量更改为continued,从而导致退出false。像这样:

    while loop
  10. 最后,您必须设置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; } } 条件,该条件不满足用户对elseab的输入。并为用户编写适当的消息并再次循环。

    c

答案 1 :(得分:0)

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

首先:

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

答案 2 :(得分:0)

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

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

}