java - 在 Java 中跳出 while 循环

标签 java loops while-loop

我真的不明白为什么它没有跳出循环。这是我的程序:

public static void main(String[] args) {

    Scanner input = new Scanner (System.in);

    double tution, rate, r, t, realpay, multiply, start;
    start = 0;

    while(start != -1)
    {
        System.out.print("Press 1 to start, -1 to end: ");
        start = input.nextDouble();

        System.out.print("Please enter the current tution fee for the year: ");
        tution = input.nextDouble();

        System.out.print("Enter in the amount of interest: ");
        rate = input.nextDouble();

        r = 1 + rate;

        System.out.print("Please enter the number of years: ");
        t = input.nextDouble();
        multiply = Math.pow(r,t);
        realpay = tution * multiply;

        System.out.println("the cost of your tution fee: " + realpay);

        if (start == -1)
        {
            break;
        }
    }
}

你能告诉我它有什么问题吗?

最佳答案

阅读开始后需要移动break

start = input.nextDouble();
if (start == -1) {
    break;
}

Else 程序将继续并在循环结束时中断,即使您输入了 -1

关于java - 在 Java 中跳出 while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12994994/

相关文章:

java - Visual Studio 代码,Java 扩展,如何将 JAR 添加到类路径?

c - 为什么我的循环即使有条件也会终止?

java - 为什么扫描仪不提示我输入?

java - java中多个条件的while循环

java - 我将如何使用 while 循环来获取多个输入?

c - 为什么即使值为真,我的 while 循环也会继续执行?

java - Jenkins jdk 经理不见了

java - 使用 Objectmapper JAVA 解析 JSON

java - 尝试在 webdriver.io 项目中打开 allure-reports 时出现异常 java.lang.UnsatisfiedLinkError

r - ggplot2 : printing multiple plots in one page with a loop