用于测试字符是否为大写/小写/数字/元音的Java程序

Prn*_*nth 10 java lowercase uppercase

正如我之前所说,如何测试输入的字符是否是参数之一?我编写了这段代码,但它似乎运行得不好(或根本没有),但没有错误.另外,我需要使用我在这里使用的基本代码.它用于学校,如果我们使用他们没有教过我们的东西(学校),我们就会失去分数.

class doody
 {
  public static void main(String[] args)
  { char i;
    char input='D';

    for(i='A';i<='Z';i++)//check if uppercase
    {
        if(input==i){
            System.out.println("Uppercase");
            switch(input){
            case 'A':
            case 'E':
            case 'I':
            case 'O':
            case 'U':
                System.out.println("Vowel"); break;
            default: System.out.println("Not a vowel"); break;}
            }

        for(i='a';i<='z';i++)//check if lowercase
        {
            if(input==i){
                System.out.println("Lowercase");
                switch(input){
                case 'a':
                case 'e':
                case 'i':
                case 'o':
                case 'u':
                System.out.println("Vowel"); break;
                default: System.out.println("Not a vowel"); break;
                }}


        for(i='0';i<='9';i++)//check if number
        {
            if(input==i)
                System.out.println("Number");
        }

    }

}}}
Run Code Online (Sandbox Code Playgroud)

编辑:这是我今天汇集的一些代码.更简单.我不知道为什么我之前没有发生这种情况.可能是因为我昏昏沉沉,已经很晚了.

class doody
{
 public static void main(String[] args)
 {  
    char input='$';//input here.

    boolean lorn=false;
    if(input>='a'&&input<='z')
        {System.out.println("Lowercase");
            lorn=true;
        if(input=='a')System.out.println("Vowel.");
        if(input=='e')System.out.println("Vowel.");
        if(input=='i')System.out.println("Vowel.");
        if(input=='o')System.out.println("Vowel.");
        if(input=='u')System.out.println("Vowel.");
        }

    if(input>='A'&&input<='Z')
        {System.out.println("Uppercase");
            lorn=true;
        if(input=='A')System.out.println("Vowel.");
        if(input=='E')System.out.println("Vowel.");
        if(input=='I')System.out.println("Vowel.");
        if(input=='O')System.out.println("Vowel.");
        if(input=='U')System.out.println("Vowel.");
        }

    if(input>='0'&&input<='9')
        {
            lorn=true;
            System.out.println("Number");
        }

    if(lorn==false)System.out.println("It is a special character");
 }
}
Run Code Online (Sandbox Code Playgroud)

Pra*_*ate 12

您的代码中不需要for循环.

以下是如何重新实现方法的方法

  • 如果输入在'A'和'Z'之间,则为大写
  • 如果输入在'a'和'z'之间,则为小写
  • 如果输入是'a,e,i,o,u,A,E,I,O,U'之一的元音
  • 其他辅音

编辑:

以下是提示您继续,以下代码片段intchars 提供值

System.out.println("a="+(int)'a');
System.out.println("z="+(int)'z');
System.out.println("A="+(int)'A');
System.out.println("Z="+(int)'Z');
Run Code Online (Sandbox Code Playgroud)

产量

a=97
z=122
A=65
Z=90
Run Code Online (Sandbox Code Playgroud)

这里是你如何检查是否一个数x之间存在两个数字说ab

// x greater than or equal to a and x less than or equal to b
if ( x >= a && x <= b ) 
Run Code Online (Sandbox Code Playgroud)

在比较期间,chars可以被视为数字

如果你能结合这些提示,你应该能够找到你想要的东西;)


Nat*_*tix 12

如果不是一门功课,你可以使用现有的方法,如Character.isDigit(char),Character.isUpperCase(char)Character.isLowerCase(char)这是一个有点"聪明",因为他们不只是在ASCII操作,而且在不同的字符集.

static final char[] VOWELS = { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U' };

static boolean isVowel(char ch) {
    for (char vowel : VOWELS) {
        if (vowel == ch) {
            return true;
        }
    }
    return false;
}

static boolean isDigit(char ch) {
    return ch >= '0' && ch <= '9';
}

static boolean isLowerCase(char ch) {
    return ch >= 'a' && ch <= 'z';
}

static boolean isUpperCase(char ch) {
    return ch >= 'A' && ch <= 'Z';
}
Run Code Online (Sandbox Code Playgroud)


Che*_*yar 7

在Java中:Character类有静态方法叫做isLowerCase(Char ch)ans isUpperCase(Char ch),Character.isDigit(Char ch)给你布尔值,基于你可以轻松实现你的任务

例:

String abc ="HomePage";

char ch = abc.charAt(i); // here i= 1,2,3......

if(Character.isLowerCase(ch))
{
   // do something :  ch is in lower case
}

if(Character.isUpperCase(ch))
{
   // do something : ch is in Upper case
}

if(Character.isDigit(ch))
{
  // do something : ch is in Number / Digit
}
Run Code Online (Sandbox Code Playgroud)


Dan*_*ker 0

您似乎将大写和小写字母从后到前。AZ 较高,az 较低。虽然不完全有效 - 存在反转的情况例外,但我认为它应该正确输出。