简体   繁体   中英

Method won't read user input and do-while loop won't skip a line

I'm making a program for a project and I don't understand why does the method won't read user input even if my variable is declared as an instance variable and also why does the do-while loop won't skip a certain line if the user will be choosing "No" for an answer.

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

You need to ask for the number of liters before running diesel() or unleaded() . Otherwise the value will default to 0 .

You also need to change your do-while loop so that it doesn't run if they selected No , and so that it doesn't go into an infinite loop if they selected Yes .

Here's how you could change it:

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();

First i want to assume the console object is declared as follows:

Scanner console = new Scanner(System.in);

with that said, your choice variable is not being re-evaluated in the do-while loop, and even if you put the prompt message in the do code block, the while loop will be evaluating to true if the user selects no(2) because of your condition:

while(choice == 2);

also, the do-while loop always evaluates the code block in the do segment before checking the while condition so regardless of what your user selects at the first instance, the do code block will still be evaluated before the while condition is checked, so a better approach will be to use a while loop instead thus you should have the following:

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

Hence the final code should look something like this noting that you have to ask for liters first before evaluating diesel or unleaded:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM