Java do-while loop isn't working

泪湿孤枕 提交于 2019-12-02 08:25:38

You are using an assignment in your while expression:

while(work = false);

You can replace with

while(work == false);

or better

while(!work);

If variables halp and work are not used anywhere else, they could be eliminated giving you:

do {
   System.out.println("What level is your fort?");
   Scanner sc = new Scanner(System.in);
   try {
    fortLevel = Integer.parseInt(sc.nextLine());
   } catch (NumberFormatException e) {
     System.out.println("Numbers only, 0-20");
   }

} while (fortLevel < 0 || fortLevel > 20);
while(work = false); // here you are assigning false to work

should be

while(work == false); //here you are checking if work is equal to false
  • = an assignment operator used to assign value
  • == an equality operator used to check if two operands have same value.

As work is boolean you could even just use this:

while(!work)

You could also do this:

if(!work) {break;}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!