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

자바'=/='자바 스크립트 – VisioN

답변

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."); 
    } 
} 

당신은 항상 nextInt()nextLine()을 수행해야합니다. 개줄은 nextInt()에서 삼키지 않았으므로 다음과 같은 nextLine()이 삼키고 있지만 원하는 것은 아닙니다.

+0

감사합니다. addiotnal 설명을 여기에 발견 : http://stackoverflow.com/questions/7056749/scanner-issue-when-using-nextline-after-nextxxx –