扫描仪不会带两个.nextLine();声明

时间:2015-02-11 01:54:47

标签: java string input java.util.scanner

public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Type a string (at least 10 characters): ");
        String str = in.nextLine();
        System.out.println("Give me two numbers, that represent the index of the first character (inclusive) and last character (exclusive) to extract.");
        System.out.print("1: ");
        int fHold = in.nextInt();
        System.out.print("2: ");
        int sHold = in.nextInt();
        String chunk = str.substring(fHold, sHold);
        System.out.print("What do you think the characters between that range are?: ");
        String guess = in.nextLine();

        if(guess.equals(chunk)){
            System.out.println("You guessed right!");
        } else {
            System.out.println("You guessed wrong!");
        }

一切正常,直到最后一个被跳过的输入。这是输出。

Type a string (at least 10 characters): Hello There
Give me two numbers, that represent the index of the first character (inclusive) and last character (exclusive) to extract.
1: 2
2: 4
What do you think the characters between that range are?: You guessed wrong!

但是,如果我制作第二个扫描仪并进行猜测= in2.nextLine(),那么它的工作没有问题。我不是大声用一个扫描仪接听两次.nextLine()吗?

0 个答案:

没有答案