简体   繁体   English

如何修改我的代码以只接受某种数据类型?

[英]How can I modify my code to only accept a certain data type?

What can I do to modify this, is there any java function for that?我该怎么做才能修改它,是否有任何 java 函数? What needs to be done so that it only accepts characters and returns an error message for other data types?需要做什么才能使其只接受字符并返回其他数据类型的错误消息?

import java.util.*;
    public class problem5
    {
      public static void main(String[] args)
      {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a letter from the alphabet: ");
        char letter = in.next(".").charAt(0);
        char vowels[] = {'A','E','I','O','U','a','e','i','o','u'};
        int vowelcount = 0;
        for (int i = 0; i < 10; i++)
        {
          if (vowels[i] == letter)
          {
            vowelcount++;
          }
        }
        if (vowelcount > 0)
        {
          System.out.println("You entered a vowel.");
        }
        else
        {
          System.out.println("You entered a consonant.");
        }
      }
    }

I need to reject input that has more than 1 char – Nico Dela Cruz我需要拒绝超过 1 个字符的输入 – Nico Dela Cruz

You just need to check the length of your input您只需要检查输入的长度

String input = in.next(".");
if(input.length == 1){
    char letter = input.charAt(0);
    ...
}

Add an else if you want to add an error message of some sort.如果要添加某种错误消息,请添加else

To check the input to only accept letter , you have Character.isLetter(char) to check every "letter" in UNICODE for you.要检查仅接受letter的输入,您可以使用Character.isLetter(char)来检查 UNICODE 中的每个“字母”。

If you want to only accept a range of az and/or AZ , you can do it yourself with an if condition or using regex.如果您只想接受az和/或AZ的范围,您可以使用 if 条件或使用正则表达式自行完成。

Wrap your loop in a statement such as:将循环包裹在如下语句中:

if (Character.isLetter(letter)){

and put and else clause at the end for your error并在最后放置和else子句以解决您的错误

Edit:编辑:

OP has changed their question slightly, so you can either: OP 稍微改变了他们的问题,因此您可以:

-Accept only the first character entered: - 只接受输入的第一个字符:

char letter = in.next().trim().charAt(0);

-Or as AxelH said above, only proceed if user enters one char: - 或者如 AxelH 上面所说,只有在用户输入一个字符时才继续:

if(input.length == 1){
    char letter = input.charAt(0);

暂无
暂无

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

相关问题 如何将方法限制为仅接受具有特定限制的参数? - How can I limit a method to only accept parameters with certain restrictions? 如何修改我的信用卡验证程序以仅检查我上次检查卡类型的 function 中的有效号码? - How can I modify my credit card validator program to check only for valid numbers in my last function that check the card type? 我如何修改我的代码,以免死锁? - How can i modify my code so it will be safe from deadlock? 如何限制arraylist在泛型之前只接受特定类型的对象 - How can I restrict the arraylist to accept only a specfic type of object prior to generic 当我将我的数据从 java 代码添加到 MYSQL 服务器时,它不接受 Null 值,如何使其接受 Null 值,如何使其接受 ZBBB93EF26E3C101 值? - When I Add my data from java code to MYSQL Server its not Accept Null Values, how to Make it to accept Null Values? 如何修改我的代码以使用前置摄像头扫描二维码 - How I can modify my code to can use front camera for scanning QR code 我不能限制我的程序只接受二进制数 - I can't restrict my program to accept only binary numbers 我需要修改我的代码以在我的 POST 请求中接受 json 对象的多个列表。 我们如何实现这一点? 任何建议都会帮助我 - I need to modify my code to accept multiple list of json objects in my POST request. How do we implement this ? Any suggestion would help me 如果我只能获取某个类的对象,而我的方法只能在 sublass 上工作,如何避免 instanceof - How to avoid instanceof if I can only obtain objects of a certain class, but my method can only work on the sublass 当用户输入不是 2 个特定字符串(选项)中的 1 个时,如何让我的 output 只显示特定消息? - How can I get my output to only display a certain message when the users input is not 1 of 2 certain strings (options)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM