我的程序中的第二个扫描程序不会将输入值放入我的整数?

时间:2017-10-03 23:18:03

标签: java input while-loop int java.util.scanner

我将立即发布我的代码并询问下面的问题。

    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 个答案:

答案 0 :(得分: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");
        }

    }

}