[英]Java loop stops working after second try
I am trying to make a console game (like Oregon trails) and I had a problem with the loop but was solved later. 我试图制作一个主机游戏(如俄勒冈步道),但循环出现问题,但后来解决了。
**this is just part of the code of the whole game. **这只是整个游戏代码的一部分。
Now the problem is, the loop doesn't work after the second try; 现在的问题是,循环在第二次尝试后不起作用。 here is the code: 这是代码:
int o1, o2, o3, o4, o5, o6, o7, o8, o9;
boolean thugloop = false;
System.out.println("Woah, you beat the thug; but now the thug is angry and he won't rest until he beats you.");
Thread.sleep(1000);
thugloop = true;
System.out.println("Pick your numbers again");
while(thugloop = true){
o4=reader.nextInt();
o5=reader.nextInt();
o6=reader.nextInt();
o7=reader.nextInt();
o8=reader.nextInt();
int sumhop2 = o4+o5+o6+o7+o8;
int angrythug= 3 + (int)(Math.random()*85);
if(sumhop2>angrythug){
Thread.sleep(1000);
System.out.println("Woah, you are really good at this!");
System.out.println("But the thug is getting angrier. Put in 5 more numbers:");
o4=reader.nextInt();
o5=reader.nextInt();
o6=reader.nextInt();
o7=reader.nextInt();
o8=reader.nextInt();
**sumhop2 = o5 + o5 + o7 + o8;
angrythug= 3 + (int)(Math.random()*85);**
}else if(sumhop2<angrythug){
//other code goes here.
break;
}
}
So it works the first time, but when it says "the thug is getting angrier. Put in 5 more.." and I put in 5 more numbers, nothing happens, it's just a blinking cursor there. 所以它第一次起作用,但是当它说“暴徒越来越生气。再输入5个..”,而我又输入5个数字时,什么也没发生,它只是一个闪烁的光标。
How can I fix this error? 如何解决此错误?
Thanks! 谢谢!
Once you enter 5 more numbers, you reach the start of the loop, which requires 5 more numbers to be input. 输入另外5个数字后,您将到达循环的起点,这需要再输入5个数字。 Hence the blinking cursor. 因此,光标闪烁。
Perhaps the first 5 inputs should be requested before the loop begins : 也许应该在循环开始之前请求前5个输入:
o4=reader.nextInt();
o5=reader.nextInt();
o6=reader.nextInt();
o7=reader.nextInt();
o8=reader.nextInt();
int sumhop2 = o4+o5+o6+o7+o8;
int angrythug= 3 + (int)(Math.random()*85);
while(thugloop == true) {
if(sumhop2>angrythug){
Thread.sleep(1000);
System.out.println("Woah, you are really good at this!");
System.out.println("But the thug is getting angrier. Put in 5 more numbers:");
o4=reader.nextInt();
o5=reader.nextInt();
o6=reader.nextInt();
o7=reader.nextInt();
o8=reader.nextInt();
sumhop2 = o5 + o5 + o7 + o8;
angrythug= 3 + (int)(Math.random()*85);
...
after the thug is getting angrier. Put in 5 more..
the thug is getting angrier. Put in 5 more..
the thug is getting angrier. Put in 5 more..
you are accepting 5 values doing the calculation in angrythug then the if block exits you are back to while loop. 再放the thug is getting angrier. Put in 5 more..
您在angerthug中接受5个值进行计算,然后if块退出,您将返回while循环。 So print your result after the 5 intake values in if block. 因此,在if块中的5个摄入值之后打印结果。
edit: or do : 编辑:或执行:
while(sumhop2 > angrythug){
Thread.sleep(1000);
System.out.println("Woah, you are really good at this!");
System.out.println("But the thug is getting angrier. Put in 5 more numbers:");
o4=reader.nextInt();
o5=reader.nextInt();
o6=reader.nextInt();
o7=reader.nextInt();
o8=reader.nextInt();
**sumhop2 = o5 + o5 + o7 + o8;
angrythug= 3 + (int)(Math.random()*85);**
}
// for sumhop2 < angrythug code goes here
// above loop will exit when the angrythug is greater than sumhop2 //当Angrythug大于sumhop2时,以上循环将退出
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.