我正在练习在Java中使用循环,我创建了这个if/else循环来不断询问是否有另一个客户,如果用户输入是
"Yes" 然后它要求客户账单."No" 然后我将总计账单(我还没有为此编写代码,所以忽略那部分).我遇到的问题是在我输入初始账单金额之后,代码询问是否应该有另一个客户,但是过早地询问他们的账单金额,然后询问是否有另一个客户第二次?
我的主要问题是,我的结构错了吗?我怎么能让循环不输出两次和/或过早的东西?
Scanner input = new Scanner(System.in);
double total;
System.out.println("Please enter your bill amount.");
double bill0 = input.nextDouble();
for(int c = 0; c > -1; c++) {
System.out.println("Is there another customer?");
String customer = input.nextLine();
if (customer.equalsIgnoreCase("Yes")) {
System.out.println("Please enter their bill.");
double bill = input.nextDouble();
} else {
System.out.println("Okay, let's total your bill amount.");
}
}
Run Code Online (Sandbox Code Playgroud)