2017-10-03 9 views
0

나는 내 코드를 즉시 게시하고 아래 질문을 할 것입니다.내 프로그램에서 두 번째 스캐너가 정수로 입력 값을 넣지 않겠습니까?

System.out.println("Enter your starting integer: "); 
    firstInt = scnr.nextInt(); 
    System.out.println("Enter your last integer: "); 
    secondInt = scnr.nextInt(); 

    int i = firstInt; 
    while (i < secondInt) { 

첫 번째 입력은 정상적으로 처리됩니다. 하지만 secondInt에 입력하려고하면 입력을 누르면 내 while 루프로 이동하지 않을 것이며 스캐너에 막혀 있습니다. 나는 입력을 치고 더 많이 입력하기 위해 줄을 따라 내려 간다. 나는 while 회 돌이로 옮길 것을 기대한다. 이것은 아마 쉬운 수정이지만, 코딩에 꽤 새로운 것이므로 어떤 도움을 주시면 감사하겠습니다. 미리 감사드립니다!

답변

1
import java.util.Scanner; 
public class Tyler 

{ 

    public static void main(String[] args) 
    { 
     Scanner stdin = new Scanner(System.in); 
     // input first int 
     System.out.print("Enter your starting integer: "); 
     int firstInt = stdin.nextInt(); 
     //input second int 
     // consume line 
     stdin.nextLine(); 
     System.out.print("Enter your last integer: "); 
     int secondInt = stdin.nextInt(); 

     // output data 
     // There was no way to break out of your while loop so this should be done with an If/else 
     if (firstInt <= secondInt) 
     { 
      System.out.println("First number is less then second number"); 
     } 
     else 
     { 
     System.out.println("Second number is less then first number"); 
     } 

    } 

}