如何创建不等式循环(Java)

时间:2015-10-18 07:53:59

标签: 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.


                    }

                }

忽略静态空...等......代码出了什么问题?

围绕** **的代码是主要问题,这个公园不起作用;我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

你有几个问题:

分配而不是比较:

while(thugloop = true)

应该是:

while(thugloop == true) // or while(thugloop)

重新声明具有相同名称的变量:

int angrythug= 3 + (int)(Math.random()*85);

应该是

angrythug= 3 + (int)(Math.random()*85);

除此之外,你应该在某处重置thugloop为假,以便循环终止。

答案 1 :(得分:0)

首先当你做

while(thugloop = true)

你实际上是在thughloop中赋予true,它等于java中的while(true)或者定义

thugloop = true;
while(thugloop)

像这样或在你的代码中使它成为thughloop == true

还要避免无限循环接受一个像-1这样的值来退出/中断循环。

int angrythug之前if和inside if在同一范围的方法中你不能这样做:http://www.cs.umd.edu/~clin/MoreJava/Objects/local.html重用if块中的angrythug变量

相关问题