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

标签 java

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

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/

相关文章:

java - 遇到java.lang.NullPointerException

java - ClassNotFoundException 我没有成功捕获

java - 如何配置@HandlerChain 以指向JAR 文件中的处理程序链配置文件?

java - Java 程序的 JPen 替代品

java - 对最后一个参数的参数类型不精确的 varargs 方法进行非 varargs 调用;

java - 如何知道 Actor 是否空闲

java - 通过网络传输 java.lang.reflect.Proxy

java - 无法包含 spring-security-web 依赖项

java - 如何使日语字符在 JTextField 中正确显示?

java - 如何用java编写一个打印机打印程序