我正在尝试制作一个控制台游戏(如俄勒冈小道),我遇到了问题with the loop但后来解决了。
**这只是整个游戏代码的一部分。
现在问题是,第二次尝试后循环不起作用;这是代码:
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;
}
}
所以它第一次起作用,但当它说'#34;暴徒正在变得更加愤怒。再加上5个......&#34;我又输了5个数字,没有任何反应,它只是一个闪烁的光标。
如何解决此错误?
谢谢!
答案 0 :(得分:0)
再输入5个数字后,您将到达循环的开头,这需要输入5个数字。因此闪烁的光标。
也许在循环开始之前请求前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);
...
答案 1 :(得分:0)
在the thug is getting angrier. Put in 5 more..
之后你接受了5个在angrythug中进行计算的值,那么if块退出就会回到while循环。因此,在if块中的5个摄入值之后打印您的结果。
编辑:或执行:
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
//当angrythug大于sumhop2时,上面的循环将退出