Java do-while循环不起作用 [英] Java do-while loop isn't working
本文介绍了Java do-while循环不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望我的程序继续问这个问题,直到得到它可以使用的响应,特别是从0到20的数字。我在这个类上有很多其他东西,所以这里有一个小的摘录while(我已经为所有内容命名了变量)。
I want my program to keep asking the question until it gets a response it can use, specifically a number from 0 to 20. I have a lot of other stuff on this class, so here is a small excerpt where the do-while is (I have named the variables and all that for everything).
public static void main(String[] args) {
do {
halp = 1;
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"); halp = 0;
}
if(halp < 1) {
work = false;
}
if(halp > 1) {
work = true;
}
while(work = false);
}
推荐答案
您正在使用的作业你的而
表达式:
You are using an assignment in your while
expression:
while(work = false);
您可以替换为
while(work == false);
或更好
while(!work);
如果变量 halp
和工作
在其他任何地方都没有使用,它们可以被淘汰给你:
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);
这篇关于Java do-while循环不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文