用于测试字符是否为大写/小写/数字/元音的Java程序 [英] Java Program to test if a character is uppercase/lowercase/number/vowel

查看:133
本文介绍了用于测试字符是否为大写/小写/数字/元音的Java程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

As I said before, how do I test if the entered character is one of the parameters? I've written this code, but it doesn't seem to run very well(or at all), no errors, however. Also, I need to use the basic code I've used here. Its for school and we lose points if we use things they haven't taught us (darn school).

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");
        }

    }

}}}

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

Here is some code I threw together today. Much simpler. I don't know why this didn't occur to me earlier. It was probably because I was groggy, it was late.

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");
 }
}


推荐答案

你在代码中不需要for循环。

You don't need a for loop in your code.

以下是如何重新实现方法

Here is how you can re implement your method


  • 如果输入介于'A'和'Z'之间,则大写

  • 如果输入介于'a'和'z'之间,则为小写

  • 如果输入是'a,e,i,o,u,A,E,I,O,U'之一,其元音

  • Else Consonant

  • If input is between 'A' and 'Z' its uppercase
  • If input is between 'a' and 'z' its lowercase
  • If input is one of 'a,e,i,o,u,A,E,I,O,U' its Vowel
  • Else Consonant

编辑:

以下提示继续,以下代码片段为 char s int 值>

Here is hint for you to proceed, Following code snippet gives int values for chars

System.out.println("a="+(int)'a');
System.out.println("z="+(int)'z');
System.out.println("A="+(int)'A');
System.out.println("Z="+(int)'Z');

输出

a=97
z=122
A=65
Z=90

以下是检查两个数字之间是否存在 x 的数字 a b

Here is how you can check if a number x exists between two numbers say a and b

// x greater than or equal to a and x less than or equal to b
if ( x >= a && x <= b ) 

比较期间 char s可被视为数字

During comparisons chars can be treated as numbers

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

If you can combine these hints, you should be able to find what you want ;)

这篇关于用于测试字符是否为大写/小写/数字/元音的Java程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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