2015-04-30 47 views
-1

我是一名初学者程序员,目前我在这个任务上撞墙。 我必须创建一个'模拟'汽车使用的程序,所以它会:驱动,停车并填满。它所要做的第一件事是询问你的坦克能容纳多少升的气体,以及目前在坦克中有多少升。然后,它询问用户是否想要a)驾驶,b)填满,或c)停车。我需要获取一个用户输入(我已经知道该怎么做),并根据用户是输入a,b还是c,它会转到这个特定的代码块并运行它。if,else if,else语句:在代码块内

下面是关于什么的a,b的具体说明,和c具有做: 一个)驱动器: 1.进入公里的驱动量(用户输入此) 2.输出气体的许多升是如何使用情况以及储罐中剩余的气体量。 (我们假定该车采用的0.1L /公里的平均)

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

} 
+0

我没有在代码中看到任何循环?如果a,b和c是String,那么abc是什么?我从来没有看到你可以连接这样的字符串? **你甚至可以运行你的代码吗?** –

+0

让我建议你把更多的描述性名称给变量,例如,而不是'a',使用'driveSelected'之类的东西。有些东西会帮助你(和其他人)记住(a)选项是用户想要驾驶的。 – germanio

+0

别的东西: 'startingLitre - litresUsed = gasTotal;' 记住,当你给变量赋值时,目标变量在左边,源(表达式,其他变量,方法被执行)放在右边声明,如下所示: 'gasTotal = startingLitre - litresUsed;' – germanio

回答

2

我试图准确地理解您需要做按您的职位是什么。我建议在您的代码中进行以下更正:

  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的条件输入直接比较,这样你就不需要变量a, b, c所需的String值。像这样:

    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 -= litresUsed;都是等价的。

  8. 您需要将编程语句startingLitre = startingLitre + litresAdded;中的数学方程式litresUsed + litresAdded = gasTotal;修正为无意义,因为您基本上已将剩余的数量添加到坦克中。

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

您的帖子被标记为低质量答案。请提供一些解释。 –

+0

@KickButtowski我希望这会好起来 – Blip

+0

我没有投票,但我会试着研究你的答案并投票决定。 –

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

+0

如何在操作代码中解释abc? –

+0

OP应该解释她自己的代码,对我来说,似乎'abc'是为阅读用户输入而设计的。但重点是如何运行方程式,我的答案解释了这一点。 – germanio

+0

@KickButtowski:那个代码有一些错误,我同意,但我不会为他/她解决所有问题,那个练习的目的是练习和学习,对吧? – germanio

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

}