方法不会读取用户输入,并且 do-while 循环不会跳过一行



我正在为一个项目制作一个程序,我不明白为什么即使我的变量被声明为实例变量,该方法也不会读取用户输入,以及如果用户选择"否"作为答案,为什么do-while循环不会跳过某一行。

System.out.println("What Gas Brand Did You Use?");
System.out.println("1 - Diesel");
System.out.println("2 - Unleaded");
gas = console.nextInt();
if (gas == 1){
    diesel();
}
else if (gas == 2){
    unleaded();
}
System.out.println("How Many Liters Did You Cosume?");
liters = console.nextInt();
System.out.println("Do You Want to Buy Some Snack? (1 - Yes or 2 - No)");
choice = console.nextInt();

do{
System.out.println("1 - Water = 20");
System.out.println("2 - Beef Jerky = 50");
add = console.nextInt();
if (add == 1){
    water();
}
else if (add == 2){
    beef();
}
}while(choice == 2); 
fee();

public static void diesel()
{
    total = 30 * liters;    
}
public static void unleaded()
{
    total = 45 * liters;
}
public static void water()
{
    total = total + water;
}
public static void beef()
{
    total = total + beef;
}
public static void fee()
{
    System.out.println("The Total Fee Is: " + total);
}  

在运行diesel()unleaded()之前,您需要询问升数。否则,该值将默认为0

您还需要更改do-while循环,以便在他们选择No时它不会运行,并且在他们选择了Yes时它不会进入无限循环。

以下是如何更改它:

System.out.println("What Gas Brand Did You Use?");
System.out.println("1 - Diesel");
System.out.println("2 - Unleaded");
gas = console.nextInt();
System.out.println("How Many Liters Did You Cosume?");
liters = console.nextInt();
if (gas == 1) {
    diesel();
} else if (gas == 2) {
    unleaded();
}
System.out.println("Do You Want to Buy Some Snack? (1 - Yes or 2 - No)");
choice = console.nextInt();
if (choice == 1) {
    System.out.println("1 - Water = 20");
    System.out.println("2 - Beef Jerky = 50");
    add = console.nextInt();
    if (add == 1) {
        water();
    } else if (add == 2) {
        beef();
    }
}
fee();

首先,我想假设控制台对象声明如下:

Scanner console = new Scanner(System.in);

也就是说,你的选择变量不会在do-while循环中重新评估,即使你把提示消息放在do代码块中,如果用户因为你的条件而选择no(2(,while循环也会评估为true:

while(choice == 2);

此外,do-while循环总是在检查while条件之前评估do段中的代码块,因此无论用户在第一个实例中选择了什么,do-wile代码块都将在检查whire条件之前进行评估,因此更好的方法是使用while循环,因此您应该具有以下内容:

System.out.println("Do You Want to Buy Some Snack? (1 - Yes or 2 - No)");
choice = console.nextInt();
while(choice==1){
  System.out.println("1 - Water = 20");
  System.out.println("2 - Beef Jerky = 50");
  add = console.nextInt();
  if (add == 1){
    water();
  }
  else if (add == 2){
    beef();
  }
  System.out.println("Do You Want to Buy Some Snack? (1 - Yes or 2 - No)");
  choice = console.nextInt();
}

因此,最后的代码应该是这样的,注意在评估柴油或无铅汽油之前,你必须先询问升数:

Scanner console = new Scanner(System.in);
System.out.println("What Gas Brand Did You Use?");
System.out.println("1 - Diesel");
System.out.println("2 - Unleaded");
gas = console.nextInt();
System.out.println("How Many Liters Did You Cosume?");
liters = console.nextInt();
if (gas == 1){
    diesel();
}
else if (gas == 2){
    unleaded();
}

System.out.println("Do You Want to Buy Some Snack? (1 - Yes or 2 - No)");
choice = console.nextInt();
while(choice == 1){
  System.out.println("1 - Water = 20");
  System.out.println("2 - Beef Jerky = 50");
  add = console.nextInt();
  if (add == 1){
    water();
  }
  else if (add == 2){
    beef();
  }
  System.out.println("Do You Want to Buy Some Snack? (1 - Yes or 2 - No)");
  choice = console.nextInt();
}
fee();

public static void diesel()
{
    total = 30 * liters;    
}
public static void unleaded()
{
    total = 45 * liters;
}
public static void water()
{
    total = total + water;
}
public static void beef()
{
    total = total + beef;
}
public static void fee()
{
    System.out.println("The Total Fee Is: " + total);
}  

最新更新