在java中突破while循环
我真的不明白为什么它没有打破循环。这是我的节目:在java中突破while循环,java,loops,while-loop,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 t
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您需要在读取开始后移动中断
start = input.nextDouble();
if (start == -1) {
break;
}
Else程序将继续,并在循环结束时中断,即使您已输入-1测试
if (start == -1) {
break;
}
应在之后立即进行
start = input.nextDouble();
在您的例子中,实际上您正在中断while循环,但只有在执行循环体之后
还要注意,将start声明为double,然后使用==测试其值可能会带来问题。对于这样的变量,最好将其声明为int。测试
if (start == -1) {
break;
}
应在之后立即进行
start = input.nextDouble();
在您的例子中,实际上您正在中断while循环,但只有在执行循环体之后
还要注意,将start声明为double,然后使用==测试其值可能会带来问题。对于这样的变量,最好将其声明为int。将If块移到while循环之外。它不会中断,因为当它读取-1时,它无法进入if块的while循环
将其移到外部,它将断开。将If块移到while循环外部。它不会中断,因为当它读取-1时,它无法进入if块的while循环
把它移到外面,它就会碎。Oops。很抱歉我还没有看到AmitD前面的答案,这也是正确的。很抱歉我还没有看到AmitD前面的答案,这也是正确的。