有谁知道如何停止或继续循环。我想问用户你想继续(是)还是(不)?...当我在这段代码中执行此操作时,循环退出。我究竟做错了什么?我在这里先向您的帮助表示感谢。
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);
}
}
答案 0 :(得分:2)
只需将while
中的do-while
条件更改为此。
while(stop.equals("yes"));
然后,您可以移除if-else
循环中的do-while
。
// 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
答案 1 :(得分:0)
break will quit the loop
continue will basically go to the top of the loop
所以你想要的逻辑是
while (true) {
if (stop.equals("yes")) continue;
break;
}
答案 2 :(得分:0)
continue
进入do..while循环的下一次迭代; break
退出循环。切换代码中的continue
和break
。
答案 3 :(得分:0)
变化
System.out.println("Do you want to continue?");
stop = kb.nextLine();
kb.nextLine();
到
System.out.println("Do you want to continue?");
stop = kb.nextLine();
答案 4 :(得分:0)
您的问题将通过以下方式解决:
System.out.println("Do you want to continue?");
stop = kb.nextLine();
stop = kb.nextLine();
if (stop.equals("yes"))
{
continue;
} else if (stop.equals("no"))
{
break;
}
答案 5 :(得分:0)
试试这个:
System.out.println("Do you want to continue?");
// stop = kb.nextLine();
// kb.nextLine();
stop = kb.next();
答案 6 :(得分:-1)
试试这个
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();
}while(stop.equals("yes"));
}