2012-12-09 77 views
-1
while (goodInput=false) 
     { 
      try 
      { 
       System.out.println("How long is the word you would like to guess?"); 
       wordSize=scan.nextInt(); 
       while(wordSize>word.longestWord()) 
       { 
        System.out.println("There are no words that big! Please enter another number"); 
        wordSize=scan.nextInt(); 
       } 
       goodInput=true; 
      } 
      catch(InputMismatchException ime) 
      { 
       System.out.println("Thats not a number! Try again"); 
      } 

     } 

我想提示用户输入号码,但我不能让它正确运行。我希望它保持运行,直到输入正确的输入。提示用户,直到输入正确的输入类型

+0

目前会发生什么? – OmniOwl

+1

“我无法让它正常运行”是**不是一个真正的问题。 –

+0

非常感谢马克。发生了什么是它捕捉到错误,然后进入该段之后的下一行。我认为南巴里所说的是什么错误。 – jeanqueq

回答

2

的一个问题是:

while (goodInput=false) 

分配falsegoodInput成为while(false)导致循环被不while循环在所有

改变它执行

while (!goodInput) 
+0

感谢您的帮助,我现在正在递归,但这可能是一个不同的问题。 – jeanqueq

+0

@aaronbobaron:我会首先建议调试(或)System.out,并查看哪些值导致递归。如果还存在问题,请发布新问题。 – kosa

+0

再等27秒,再次感谢 – jeanqueq

0

条件需要成为

while(goodinput == false) 

你在做什么是分配false良好的输入,导致最终结果为false。请参阅以下语句的输出,

boolean a; 
System.out.println((a = false)); 

您需要equality operator那里。

0

首先,

while (goodInput=false) 

是指派falsegoodInput,你必须使用==操作tocheck如果goodInputfalse

while (goodInput==false) 

或者只是

while (!goodInput) would suffice 

这里是重不辞而别到Equality Operator在Java

0

你必须写

while (goodInput == false) 

甚至更​​好

while (!goodInput) 

,而不是

while (goodInput = false) 

的第一个goodInput值与false比较,第二个否定的值和您的版本分配falsegoodInput

相关问题