如何使 Java 中的字符输入无效?

发布于 2025-01-10 18:28:19 字数 916 浏览 1 评论 0原文

这非常简单,但我是 Java 编程语言的新手,一些指导会有所帮助。我正在制作一个列出选项的菜单。但是,我希望用户键入一个字符作为一种选项,以获得类似此处的文本,并再次将用户返回到选择。

我通过将大小写与默认值混淆来呈现错误。

import java.util.Scanner;

public class selectMenu {

    public static void main(String[] args) { 
        System.out.println("select your option:");
        System.out.println("1) showing today menu");
        System.out.println("2) showing tomorrow menu");
        
        int op = scanner.nextInt();
        
        switch(op) {
            
            case 1 -> System.out.println("TODAY MENU");
            
            case 2 -> System.out.println("TOMORROW MENU");
            
            case 3 ->
                if (option==char) {
                    System.out.println("This is an invalid option.");
                }
                
            
            default -> System.out.println("Opcion invalida.");
            
        }
}

This is extremely simple, but I am new to the Java programming language and some guidance would be helpful. I am making a menu where the options are listed. However, I would love for the user to type a character as an option to get a text like the one here and return the user to the selection again.

I present errors by confusing the case with the default.

import java.util.Scanner;

public class selectMenu {

    public static void main(String[] args) { 
        System.out.println("select your option:");
        System.out.println("1) showing today menu");
        System.out.println("2) showing tomorrow menu");
        
        int op = scanner.nextInt();
        
        switch(op) {
            
            case 1 -> System.out.println("TODAY MENU");
            
            case 2 -> System.out.println("TOMORROW MENU");
            
            case 3 ->
                if (option==char) {
                    System.out.println("This is an invalid option.");
                }
                
            
            default -> System.out.println("Opcion invalida.");
            
        }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慕巷 2025-01-17 18:28:19

您可以尝试执行以下操作:

import java.util.Scanner;
public class selectMenu
{
    public static void main(String[] args)
    { 
        Scanner in = new Scanner(System.in);

        menu: while(true)
        {
            System.out.println("select your option:");
            System.out.println("1) showing today menu");
            System.out.println("2) showing tomorrow menu");
            String op = in.nextLine();

            switch(op)
            {
                case "1":
                        System.out.println("TODAY MENU");
                        break menu;
    
                case "2":
                        System.out.println("TOMORROW MENU");
                        break menu;
    
                default:
                        try
                        {
                            Integer.parseInt(op);
                            System.out.println("You typed an int but it isn't 1 or 2");
                        }
                        catch(NumberFormatException e)
                        {
                            if(op.length() > 1) System.out.println("You typed a String");
                            else System.out.println("You typed a char");
                        }
                        break;
            }
        }
    }
}

它采用下一行而不是 nextInt() (如果它不是数字,则会抛出异常),检查答案是否为 "1"“2”,然后检查它是否是有效的 int(“3”、“4”等)。如果不是,它会检查长度,如果长度为 1,则告诉您它是一个 char,否则是一个 String

如果您不关心用户实际输入的内容(charString 等),只是他们没有输入 int,您可以移动 try-catch 围绕扫描仪输入:

import java.util.Scanner;
public class selectMenu
{
    public static void main(String[] args)
    { 
        Scanner in = new Scanner(System.in);

        menu: while(true)
        {
            System.out.println("select your option:");
        System.out.println("1) showing today menu");
        System.out.println("2) showing tomorrow menu");
            try
            {
                int op = in.nextInt();
    
            switch(op)
                {
              case 1:
                        System.out.println("TODAY MENU");
                        break menu;
        
                case 2:
                        System.out.println("TOMORROW MENU");
                        break menu;
        
              default:
                        System.out.println("int input, but not 1 or 2");
            }
            }
            catch(java.util.InputMismatchException e)
            {
                System.out.println("non-int input");
            }
            in.nextLine();
        }
    }
}

抱歉,如果缩进有点偏离,我不知道为什么会发生这种情况。

You could try doing something like this:

import java.util.Scanner;
public class selectMenu
{
    public static void main(String[] args)
    { 
        Scanner in = new Scanner(System.in);

        menu: while(true)
        {
            System.out.println("select your option:");
            System.out.println("1) showing today menu");
            System.out.println("2) showing tomorrow menu");
            String op = in.nextLine();

            switch(op)
            {
                case "1":
                        System.out.println("TODAY MENU");
                        break menu;
    
                case "2":
                        System.out.println("TOMORROW MENU");
                        break menu;
    
                default:
                        try
                        {
                            Integer.parseInt(op);
                            System.out.println("You typed an int but it isn't 1 or 2");
                        }
                        catch(NumberFormatException e)
                        {
                            if(op.length() > 1) System.out.println("You typed a String");
                            else System.out.println("You typed a char");
                        }
                        break;
            }
        }
    }
}

It takes the next line rather than nextInt() (which will throw an exception if it's not a number), checks if the answer is "1" or "2", then checks if it's a valid int ("3", "4", etc). If not, it checks the length, and tells you it's a char if the length is 1, otherwise a String.

If you don't care about what the user actually entered (char, String, etc), just that they didn't type an int, you could move the try-catch around the scanner input:

import java.util.Scanner;
public class selectMenu
{
    public static void main(String[] args)
    { 
        Scanner in = new Scanner(System.in);

        menu: while(true)
        {
            System.out.println("select your option:");
        System.out.println("1) showing today menu");
        System.out.println("2) showing tomorrow menu");
            try
            {
                int op = in.nextInt();
    
            switch(op)
                {
              case 1:
                        System.out.println("TODAY MENU");
                        break menu;
        
                case 2:
                        System.out.println("TOMORROW MENU");
                        break menu;
        
              default:
                        System.out.println("int input, but not 1 or 2");
            }
            }
            catch(java.util.InputMismatchException e)
            {
                System.out.println("non-int input");
            }
            in.nextLine();
        }
    }
}

Sorry if the indenting is a bit off, I don't know why that was happening.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文