2017-10-03 3 views
0

私のコードをすぐに投稿し、その下の質問をします。私のプログラムで2番目のスキャナは入力値を私の整数に入れませんか?

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に入力しようとすると、私はenterを押して、それはスキャナでちょうど立ち往生している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"); 
     } 

    } 

} 
関連する問題