为什么我的Java for/if/else语句会输出重复项
我正在练习使用Java中的循环,我创建了这个if/else循环,不断询问是否有其他客户,用户输入是否正确为什么我的Java for/if/else语句会输出重复项,java,if-statement,for-loop,Java,If Statement,For Loop,我正在练习使用Java中的循环,我创建了这个if/else循环,不断询问是否有其他客户,用户输入是否正确 “是”然后它要求客户支付账单 “否”然后我将合计账单(我还没有为此编写代码,所以忽略该部分) 我遇到的问题是,在我输入初始账单金额后,代码会询问是否有其他客户,但随后过早地询问他们的账单金额,然后再次询问是否有其他客户 我的主要问题是,我的结构有错吗?我如何才能使循环不两次和/或过早地输出某些内容 Scanner input = new Scanner(System.in); doubl
然后它要求客户支付账单“是”
然后我将合计账单(我还没有为此编写代码,所以忽略该部分)“否”
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.");
}
}
这个循环将(基本上)永远运行,因为c总是大于-1。
(实际上,此循环将一直运行,直到发生溢出,因为c的值太大,无法容纳为此整数分配的可用存储空间。有关详细信息,请参阅此处:)
一种更干净的方法是这样做:
String answer = "Yes";
while (answer.equals("Yes"))
{
System.out.println("Please enter your bill amount.");
double bill0 = input.nextDouble();
System.out.println("Is there another customer? (Yes or No)");
answer = input.nextLine();
}
total += bill0;
当然,如果用户输入了您不期望的输入,则需要添加错误处理。此外,这更多的是伪代码,而不是真正的实现
在此之后,while循环是您希望合计金额的时间。此外,在while循环中,您可能需要一个变量来保持总量。比如:
String answer = "Yes";
while (answer.equals("Yes"))
{
System.out.println("Please enter your bill amount.");
double bill0 = input.nextDouble();
System.out.println("Is there another customer? (Yes or No)");
answer = input.nextLine();
}
total += bill0;
行后:
double bill0 = input.nextDouble();
可能会成功。马特·琼斯(Matt Jones)是正确的-你的循环(基本上)会永远运行(溢出意味着它不会永远运行,但已经足够接近了) 当用户输入“否”时,您似乎试图打破循环。这意味着您不知道需要多少次迭代,因此while循环比for循环更适合此任务。让我们用一个while循环来实现这一点
Scanner input = new Scanner(System.in);
double total;
System.out.println("Please enter your bill amount.");
double bill0 = input.nextDouble();
//loop until the user enters the phrase "No".
while(true) {
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 if(customer.equalsIgnoreCase("No") {
System.out.println("Okay, let's total your bill amount.");
break; //End the loop
} else{
System.out.println("Sorry, unrecognized input. Please enter yes or no.");
}
}
//Do post-loop totaling and stuff.
我将使用
while
循环
while (scannerInput.equals("yes"))
//do your thing
}
一旦扫描仪输入不再等于“是”,它将退出,同时执行其他操作
for
循环更多的是遍历一组数据,而不是等待状态更改的while
。即使在循环中使用if-else语句,这仍然是for循环。没有像if这样的东西loop@ifLoop:生病的参考兄弟@DJM4:这将被称为“while循环”“此循环将永远运行,因为c始终大于-1”我怀疑,根据我的说法,这个循环应该运行2147483647+2147483647次?不要太直截了当:)重点是,它不是用户想要的,在用例中,行为可以被归类为无限循环。是的,但你应该编辑你的答案,因为它可能会被一些用户视为真正的循环你说得对,我已经更新了答案。谢谢你的澄清@维卡斯维玛
Scanner input = new Scanner(System.in);
double total;
System.out.println("Please enter your bill amount.");
double bill0 = input.nextDouble();
System.out.println("Is there another customer?");
while((String customer = input.nextLine()).equalsIgnoreCase("Yes")) {
System.out.println("Please enter their bill.");
double bill = input.nextDouble();
}
System.out.println("Okay, let's total your bill amount.");