如何创建不平等循环(Java)
我试图编写一个循环,以便程序检查总和,如果总和<随机数,那么它将再次启动,但由于出现错误,这无法工作。代码如下:如何创建不平等循环(Java),java,Java,我试图编写一个循环,以便程序检查总和,如果总和
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.");
o4=reader.nextInt();
o5=reader.nextInt();
o6=reader.nextInt();
o7=reader.nextInt();
o8=reader.nextInt();
**sumhop2 = o5 + o5 + o7 + o8;
int angrythug= 3 + (int)(Math.random()*85);**
}else if(sumhop2<angrythug){
//other code goes here.
}
}
忽略静态空隙。。等代码有什么问题
围绕****的代码是主要问题,这个公园不起作用;如何解决此问题?您有几个问题: 分配而不是比较:
while(thugloop = true)
应该是:
while(thugloop == true) // or while(thugloop)
重新声明具有相同名称的变量:
int angrythug= 3 + (int)(Math.random()*85);
应该是
angrythug= 3 + (int)(Math.random()*85);
除此之外,您应该在某个地方将thugloop重置为false,以便循环终止。首先,当您执行此操作时
while(thugloop = true)
实际上,您正在将true赋值给thughloop,这等于java中的whiletrue或define
thugloop = true;
while(thugloop)
像这样或在代码中使其thughloop==true
另外,为了避免有限循环,请接受类似-1的值,该值将退出/中断循环
int angrythug before if和inside if在同一个方法范围内,您无法执行读取:在if块内重用angrythug变量您将通过读取错误消息获得大量线索。如果你不明白,就把它贴出来。请正确缩进代码。您的IDE只需一个键盘快捷键即可实现这一点;“=”和“=”的区别是什么?@Aakarsh=true将true赋值给变量,而==true测试变量的当前值是否为true。谢谢,我成功了,但是循环本身在一段时间后就不起作用了。第一次,我进入了517的作品。第二次停止时,控制台没有响应?有什么不对劲吗?