2013-03-06 77 views
-1
//Question4 
correct = false; 
System.out.println("how many feet are in a yard?"); 
while (!correct) 
{ 
    x1 = input.nextInt(); 
    if (x1==3) 
    { 
     System.out.println("correct."); 
     correct = true; 
    } 
    else 
    { 
     System.out.println("incorrect. try again."); 
    } 
} 


    //Question5 
correct = false; 
System.out.println("What gets wetter and wetter the more it dries?"); 
while (correct == false) 
{ 

    s1 = input.nextLine(); 
    if (s1.equalsIgnoreCase("a towel")) // cheating is bad! 
    { 
     System.out.println("correct"); 
     correct = true; 
    } 

    else 
    { 
     System.out.println("incorrect. try again."); 
    } 
} 

输出 多少脚是在一个院子里? 正确。 干燥得越多,它越湿润越湿润? 不正确。再试一次。甚至在询问用户输入之前打印正在打印此输出为什么,显得格格不入

+1

Java'=/='JavaScript – VisioN 2013-03-06 23:39:55

回答

1

改变你的第一个问题是这样的:

while (!correct) 
{ 
    x1 = input.nextInt(); 
    input.nextLine(); //NEW CODE 
    if (x1==3) 
    { 
     System.out.println("correct."); 
     correct = true; 
    } 
    else 
    { 
     System.out.println("incorrect. try again."); 
    } 
} 

你必须始终做一个nextLine()一个nextInt()之后。 nextInt()没有吞下换行符,因此下面的nextLine()吞咽的并不是你想要的。

+0

好的,谢谢。在这里找到addiotnal解释:http://stackoverflow.com/questions/7056749/scanner-issue-when-using-nextline-after-nextxxx – 2013-03-06 23:50:59