Loops 当循环无原因地查找意外输入时?
我试图运行一个while循环程序,它与输入无关。它只是想告诉我计算的最终值是多少。但是,当我运行程序时,它什么也不做。它也没有结束。我对正在发生的事情感到困惑Loops 当循环无原因地查找意外输入时?,loops,input,while-loop,Loops,Input,While Loop,我试图运行一个while循环程序,它与输入无关。它只是想告诉我计算的最终值是多少。但是,当我运行程序时,它什么也不做。它也没有结束。我对正在发生的事情感到困惑 int x = 90; while (x < 100) { x += 5; if (x > 95) x -= 25; } System.out.println( "final value for x is " + x); intx=90;
int x = 90;
while (x < 100)
{
x += 5;
if (x > 95)
x -= 25;
}
System.out.println( "final value for x is " + x);
intx=90;
而(x<100)
{
x+=5;
如果(x>95)
x-=25;
}
System.out.println(“x的最终值为“+x”);
发生的情况是,循环从未停止,因此它从未打印任何内容,请尝试在循环中更改代码
你是如何意识到这一点的?
在循环时,在循环中打印一些内容:
int x = 90;
System.out.println("Before the while");
while (x < 100) {
System.out.println("Inside the while");
x += 5;
if (x > 95)
x -= 25;
}
System.out.println("final value for x is " + x);
迭代2:
x = 100
if condition is true, so x = 75
。。。因此,每当x达到100,条件将使其为75。因此,while永远不会结束。循环永远不会结束,因为x
永远不会达到100。如果您想亲自查看x
,请在循环中添加一行代码,使代码如下所示:
int x = 90;
while (x < 100) {
System.out.println("x = " + x); // More useful output here...
x += 5;
if (x > 95)
x -= 25;
}
System.out.println("final value for x is " + x);
intx=90;
而(x<100){
System.out.println(“x=”+x);//这里有更有用的输出。。。
x+=5;
如果(x>95)
x-=25;
}
System.out.println(“x的最终值为“+x”);
它总是在循环中,所以它永远不会到达println:)如果你想知道它是否有效,请将println放入循环中。你期望得到什么结果?我想这是我教授的意图。他只是想让我们知道结果是什么。我只是想确保我在执行死刑时没有做错任何事。
int x = 90;
while (x < 100) {
System.out.println("x = " + x); // More useful output here...
x += 5;
if (x > 95)
x -= 25;
}
System.out.println("final value for x is " + x);