gpt4 book ai didi

java - 在 Java 中跳出 while 循环

转载 作者:行者123 更新时间:2023-11-29 09:49:18 28 4
gpt4 key购买 nike

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

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/

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