简体   繁体   中英

Second scanner in my program wont put the input value into my integer?

I am going to post my code right away and ask the question below it.

    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) {

The first input is taken just fine. But when I try to input to secondInt, I hit enter and it wont move onto my while loop it is just stuck in the scanner. I hit enter and i just move down a line to input more. I watn it to move to my while loop. This is probably an easy fix but im pretty new to coding so any help would be appreciated. Thanks in advance!

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

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM