简体   繁体   English

对于 Java,如果我想做一个 if 语句以确保输入只包含一个字符,我应该如何进行?

[英]For Java, if I want to do an if statement to make sure the input only takes in one character, how should I proceed?

I am trying to do a while loop to get user input and make sure what they put is a character only, so if they do ab, it will ask them to do another user input instead of taking the first letter they entered as an answer.我正在尝试做一个while循环来获取用户输入并确保他们输入的只是一个字符,所以如果他们做ab,它会要求他们做另一个用户输入,而不是把他们输入的第一个字母作为答案。 Can I do this with a try catch such as the one below?我可以使用如下所示的 try catch 来执行此操作吗? It is not working right now and I am not sure why.它现在不工作,我不知道为什么。

Thanks for your help!谢谢你的帮助!

            boolean single = true;
        
                while(single) {
                
                    try {
                        if(letter.length()==1) {
                            break;
                        } else {
                            System.out.println("Please enter a single character only!");
                        }
                    } catch (Exception e) {
                      System.out.println("Please enter a single character only!");
                    }
                }

I am trying to do a while loop to get user input and make sure what they put is a character only, so if they do ab, it will ask them to do another user input instead of taking the first letter they entered as an answer.我正在尝试做一个while循环来获取用户输入并确保他们输入的只是一个字符,所以如果他们做ab,它会要求他们做另一个用户输入,而不是把他们输入的第一个字母作为答案。 Can I do this with a try catch such as the one below?我可以使用如下所示的 try catch 来执行此操作吗? It is not working right now and I am not sure why.它现在不工作,我不知道为什么。

Thanks for your help!谢谢你的帮助!

            boolean single = true;
        
                while(single) {
                
                    try {
                        if(letter.length()==1) {
                            break;
                        } else {
                            System.out.println("Please enter a single character only!");
                        }
                    } catch (Exception e) {
                      System.out.println("Please enter a single character only!");
                    }
                }

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

相关问题 如何确保输入不是 null 并且只输入了一个字符? - How do I make sure that the input is not null and that only one character is being entered? 如何确保在 Java 中只选择了一个 JRadioButton? - How Do I Make Sure Only One JRadioButton Is Selected In Java? 如何使用 Java 确保输入 SQL 语句具有有效的列名? - How do I make sure an input SQL Statement has valid column names using Java? 如何确保仅启动一个线程 - How do I make sure only one thread gets started 如何确保if语句内的指令(也位于for循环内)仅执行一次? - How do I make sure the instruction inside an if statement, which is also inside a for loop, gets executed only once? 如果在Java中,如何继续进行其他操作? - How do I proceed with else if in java? 如何确保用户仅输入整数类型和大于0的整数? - How do I make sure User input integer type only and integer that is greater than 0? 如何确保只能执行我的程序的一个实例? - How do I make sure only one instance of my program can be executed? 如何确保仅初始化一个实例,但从另一个类初始化? - How do I make sure to initialize only one Instance but from another class? 如何确保java程序与Java Enviornment 6兼容? - How do I make sure that a java program is compatible with Java Enviornment 6?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM