简体   繁体   English

用户输入中挂起的令牌不允许我继续执行程序

[英]Hanging token from user input is not allowing me to proceed in my program

My program is not allowing me to enter user input if i do not enter a number and i want to go through the program again, it think its due to a hanging token somewhere but i cannot seem to find it. 如果我不输入数字并且我想再次浏览该程序,我的程序将不允许我输入用户输入,它认为是由于某个地方悬挂了令牌,但我似乎找不到它。

import java.util.Scanner;

public class LessonTwo {
    static Scanner userInput = new Scanner(System.in);
    public static void main(String[] args) {
        char answer = ' ';
        do {
            System.out.print("Your favorite number: "); 
            if (userInput.hasNextInt()) {
                int numberEntered = userInput.nextInt();
                userInput.nextLine();

                System.out.println("You entered " + numberEntered);

                int numEnteredTimes2 = numberEntered + numberEntered;
                System.out.println(numberEntered + " + " + numberEntered
                        + " = " + numEnteredTimes2);

                int numEnteredMinus2 = numberEntered - 2;
                System.out.println(numberEntered + " - 2 " + " = "
                        + numEnteredMinus2);

                int numEnteredTimesSelf = numberEntered * numberEntered;
                System.out.println(numberEntered + " * " + numberEntered
                        + " = " + numEnteredTimesSelf);

                double numEnteredDivide2 = (double) numberEntered / 2;
                System.out.println(numberEntered + " / 2 " + " = "
                        + numEnteredDivide2);

                int numEnteredRemainder = numberEntered % 2;
                System.out.println(numberEntered + " % 2 " + " = "
                        + numEnteredRemainder);

                numberEntered += 2; // *= /= %= Also work
                numberEntered -= 2;

                numberEntered++;
                numberEntered--;

                int numEnteredABS = Math.abs(numberEntered); // Returns the
                int whichIsBigger = Math.max(5, 7);
                int whichIsSmaller = Math.min(5, 7);

                double numSqrt = Math.sqrt(5.23);

                int numCeiling = (int) Math.ceil(5.23);
                System.out.println("Ceiling: " + numCeiling);

                int numFloor = (int) Math.floor(5.23);
                System.out.println("Floor: " + numFloor);

                int numRound = (int) Math.round(5.23);
                System.out.println("Rounded: " + numRound);

                int randomNumber = (int) (Math.random() * 10);
                System.out.println("A random number " + randomNumber);

            } else {
                System.out.println("Sorry you must enter an integer");
            }
            System.out.print("Would you like to try again? ");

            answer = userInput.next().charAt(0);

        }while(Character.toUpperCase(answer) == 'Y');
        System.exit(0);
    }
}

Yes you are right you need to consume the characters first after the user inputted character in the nextInt before allowing the user to input data again 是的,你是对的,你需要将用户输入后第一消耗字符, characternextInt再次允许用户输入数据之前

just add this in your else block and it will work: 只需将其添加到您的else块中,它将起作用:

    else {
        System.out.println("Sorry you must enter an integer");
        userInput.nextLine(); //will consume the character that was inputted in the `nextInt`

    }

EDIT: 编辑:

change this: 改变这个:

answer = userInput.next().charAt(0);

to: 至:

answer = userInput.nextLine().charAt(0);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试从带有SSL的Twitch.tv API获取令牌时,为什么Java程序挂起? - Why is my Java program is hanging when trying to get a token from the Twitch.tv API with SSL? Java简单程序不允许接受用户输入 - Java simple program not allowing to take user input 为什么我的ItemListener程序不允许我从Java中的两个不同的JComboBox中选择一个选项? - Why is my ItemListener program not allowing me to select an option from two different JComboBoxes in Java? 从用户那里读入输入文本 Yes/No Boolean 以继续下一步 - Read in input text Yes/No Boolean from user to proceed the next step 我的程序不允许我在构造函数中要求用户输入 - My program doesn't allow me to ask for user input inside a constructor 计时器类。 接受用户输入而不挂JForm - Timer Class. Accepting Input from user without hanging the JForm 如何防止我的程序由于用户输入而崩溃 - How to prevent my program from crashing due to a user's input 为什么我的java程序不接受用户的字母输入? - Why will my java program not accept alphabetical input from the user? 如何在允许通过我的Java程序进行操作的同时,保护网络驱动器上的某些文件免于直接用户操作? - How to protect certain files on a network drive from direct user manipulation while allowing manipulation through my Java program? java中只允许从用户输入中获取数字 - Only allowing numbers to be taken from user input in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM