如果用户键入的数字不可用,则会显示以下消息java控制台 [英] if a user types in a number that isn't available the following message is printed java console

查看:75
本文介绍了如果用户键入的数字不可用,则会显示以下消息java控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,由于我是Java的新手,我需要当前正在开发的程序的帮助.

Hi I need help on a program I am currently working on since I am a noob with java.

基本上,这是一个简单的控制台程序,提示用户选择3个选项:退出",加载第一个播放列表"和加载第二个播放列表". 我需要添加某些功能,例如当用户键入字母而不是数字时,将出现一条消息,提示用户键入数字而不是字母.并且,如果用户键入的数字不是1,2,3,则将显示一条消息,提示用户选择这些数字之一.

It is basically a simple console program that prompts the user to choose 3 options: Exit, Load First Playlist and Load Second Playlist. I need to add certain functions such as when the user types a letter instead of a number, a message will appear prompting the user to type in a number instead of a letter. And if the user typed in a different number instead of 1,2,3 a message will appear prompting the user to choose one of those numbers.

private ExampleOption getOptionFromUser() {

    Scanner in = new Scanner(System.in);
    while (true) {
        int num = in.nextInt();

        for (ExampleOption option : options) {
            if (option.getOptionNumber() == num) {
                return option;
            }
        }
    }
}

谢谢

推荐答案

您需要做的第一件事是允许用户输入任何内容,Scanner#nextLine将允许您获取用户输入的所有输入.然后,您可以使用Integer.parseInt解析结果,并基于该操作的成功,可以返回/继续或显示某种错误消息.

The first thing your need to do, is allow the user to enter anything, Scanner#nextLine will allow you to get all of the input the user enters. You can then use Integer.parseInt to parse the result and based on the success of that operation, you can either return/continue or show some kind of error message.

public int getInt(Scanner scanner, String prompt) {
    int result = -1;
    boolean validOption = false;
    do {
        System.out.print(prompt);
        String text = scanner.nextLine();
        try {
            result = Integer.parseInt(text);
            validOption = true;
        } catch (NumberFormatException exp) {
            System.err.println(text + " is not a valid integer, please try again");
        }
    } while (!validOption);
    return result;
}

为了说服,您可以将其包装在一个方法中并重新使用代码.准备好后,您甚至可以添加范围检查(以查看该值是否在可接受的范围内)

For convince, you could wrap it in a method and re-use the code. When you're ready, you could even add range checking (to see if the value is within the acceptable range)

同样,您也可以使用nextInt ...

Equally, you could also just make use of nextInt...

public int getInt(Scanner scanner, String prompt) {
    int result = -1;
    boolean validOption = false;
    do {
        System.out.print(prompt);
        try {
            result = scanner.nextInt();
            validOption = true;
        } catch (InputMismatchException exp) {
            if (scanner.hasNextLine()) {
                scanner.nextLine();
            }
            System.err.println("You did not enter a valid integer value, please try again");
        }
    } while (!validOption);
    if (scanner.hasNextLine()) {
        scanner.nextLine();
    }
    return result;
}

但是您需要记住使用nextLine清除无效字符流.使用此选项,我可以输入123 456,但仅读取123,这意味着456将保留在流中,并且该方法将返回123,这可能是用户期望的.

But you need to remember to use nextLine to clear the stream of the invalid character. With this option, I could enter 123 456, but only 123 would be read, meaning that 456 would remain in the stream and the method would return 123, which might be what the user expects.

这篇关于如果用户键入的数字不可用,则会显示以下消息java控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆