这是代码。基本上在我输入第二个客户账单金额后,它会过早地给我一个小费,并询问是否有另一个客户。我想让它问我是否有其他客户,但我不想让它询问提示,直到我输入“n”。有什么想法吗?
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 + ".");
}
}
}
答案 0 :(得分:0)
你的逻辑扭曲了。
更好地移动循环内的完整逻辑并检查其末尾的退出(不是开始):
do
read bill amount
read tip amount
read next customer
while (wants to continue)
答案 1 :(得分:0)
由于else if
,您可以输入最后一个客户的帐单金额或小费百分比,但不能同时输入两者。相反,尝试这个逻辑:
while( there are more individual customer amounts to enter ) {
enter next customer amount
}
get tip percentage
show final bill
请注意,在while循环之后输入提示百分比,因为它只是一次性输入。