这是密码。基本上,在我输入第二个客户账单金额后,它会提前给我看一条提示,并询问是否还有其他客户。我要它问我是否有其他客户,但我不希望它问有关小费,直到我输入"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 + ".");
}
}
}发布于 2014-09-27 23:12:52
由于else if,您可以输入最后一个客户的账单金额或小费百分比,但不能同时输入两者。相反,尝试以下逻辑:
while( there are more individual customer amounts to enter ) {
enter next customer amount
}
get tip percentage
show final bill注意,提示百分比是在while循环之后输入的,因为它只是一个一次性条目。
发布于 2014-09-27 23:09:39
你的逻辑扭曲了。
最好在循环中移动完整的逻辑,并检查是否在循环结束时退出(而不是开始):
do
read bill amount
read tip amount
read next customer
while (wants to continue)https://stackoverflow.com/questions/26079972
复制相似问题