Java 提示计算器语句混淆
这是代码。基本上,在我输入第二个客户账单金额后,它会过早地给我读提示,并询问是否还有其他客户。我想让它问我是否还有其他客户,但我不想让它在我输入“n”之前询问小费。有什么想法吗Java 提示计算器语句混淆,java,if-statement,calculator,Java,If Statement,Calculator,这是代码。基本上,在我输入第二个客户账单金额后,它会过早地给我读提示,并询问是否还有其他客户。我想让它问我是否还有其他客户,但我不想让它在我输入“n”之前询问小费。有什么想法吗 import java.util.Scanner; public class H3_TipCalc { public static void main(String[] args) { Scanner input = new Scanner (System.in); System.out.pr
import java.util.Scanner;
public class H3_TipCalc {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.println("Please enter your bill amount.");
double bill = input.nextDouble();
String multiplecust = ("Y");
//int mc=1;
while (multiplecust.equalsIgnoreCase("y"))
{
Scanner usrin = new Scanner (System.in);
System.out.println("Is there another customer? y or n?");
multiplecust = usrin.nextLine();
//mc++;
if (multiplecust.equals("y")){
System.out.println("Please enter their bill amount.");
}
else if (multiplecust.equals("n")){
System.out.println("What tip prcentage would you like to use? Enter as a decimal.");
}
double tip = usrin.nextDouble();
System.out.println("Your tip owed will be " + bill*tip + ".");
}
}
}
你的逻辑被扭曲了 最好将完整的逻辑移到循环中,并在循环结束时(而不是开始时)检查退出:
由于
的else if
,您可以输入最后一位客户的账单金额或小费百分比,但不能同时输入两者。相反,尝试以下逻辑:
while( there are more individual customer amounts to enter ) {
enter next customer amount
}
get tip percentage
show final bill
请注意,小费百分比是在while循环之后输入的,因为它只是一次性输入
while( there are more individual customer amounts to enter ) {
enter next customer amount
}
get tip percentage
show final bill