2013-03-06 109 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