gpt4 book ai didi

java - 如何停止循环然后继续?

转载 作者:行者123 更新时间:2023-12-01 13:50:49 25 4
gpt4 key购买 nike

有谁知道如何停止或继续循环。我想问用户你想继续(是)还是(否)?...当我在这段代码中执行此操作时,循环退出。我究竟做错了什么?我在这里先向您的帮助表示感谢。

import java.util.Scanner;
public class salarywithdoloop {
public static void main(String args[]) {
Scanner kb = new Scanner(System.in);
int salary = 0;
double federaltax = 0;
double netsalary = 0;
double totaltax = 0;
double statetax = 0;
int over_100000 = 0;
int between50_100000 = 0;
int between25_50000 = 0;
int below25000 = 0;
String stop = "";
int count = 0;
do {
System.out.println("Please Input your salary");
salary = kb.nextInt();
if (salary >= 100000) {
statetax = salary * .05;
federaltax = salary * .20;
netsalary = statetax + federaltax;

} else {
statetax = salary * .05;
federaltax = salary * .15;
netsalary = statetax + federaltax;

}
if (salary >= 100000) {
over_100000 = over_100000 + 1;
} else if (salary >= 50000 && salary <= 100000) {
between50_100000 = between50_100000 + 1;
} else if (salary >= 25000 && salary <= 50000) {
between25_50000 = between25_50000 + 1;
} else {
below25000 = below25000 + 1;
}

System.out.println("Federal tax :" + federaltax);
System.out.println("netsalary :" + netsalary);
System.out.println("statetax :" + statetax);
System.out.println("salary :" + salary);
System.out.println("Over 100000: " + over_100000);
System.out.println("Between 50000 and 100000: " + between50_100000);
System.out.println("Between 25000 and 50000: " + between25_50000);
System.out.println("Below 25000:" + below25000);

System.out.println("Do you want to continue?");
stop = kb.nextLine();
kb.nextLine();

if (stop.equals("yes")) {
continue;
} else if (stop.equals("no")) {
break;
}
} while (0 <= count);
}
}

最佳答案

只需将 do-while 中的 while 条件更改为此即可。

while(stop.equals("yes"));

然后您可以删除 do-while 循环中的 if-else

// The below if-else is not required at all, I commented it, but you can delete it.
//if (stop.equals("yes")) {
//continue;
//} else if (stop.equals("no")) {
//break;
//}

并且您需要反转 2 个 readLine() 语句。

kb.nextLine(); // line 1, this will consume the enter
stop = kb.nextLine(); // line 2, // this will actually get the next user input

关于java - 如何停止循环然后继续?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19969743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com