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

发布于 2024-12-28 11:33:05 字数 1432 浏览 0 评论 0原文

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

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

发布评论

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

评论(8

望她远 2025-01-04 11:33:05

如果不是作业,您可以使用现有方法,例如 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';
}

If it weren't a homework, you could use existing methods such as Character.isDigit(char), Character.isUpperCase(char) and Character.isLowerCase(char) which are a bit "smarter", because they don't operate only in ASCII, but also in various charsets.

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';
}
给妤﹃绝世温柔 2025-01-04 11:33:05

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

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

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

编辑:

这里是提示您继续,以下代码片段给出了char的int

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 greater than or equal to a and x less than or equal to b
if ( x >= a && x <= b ) 

在比较期间检查两个数字(例如ab)之间是否存在数字x< code>chars 可以被视为数字

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

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

Here is how you can re implement your method

  • 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

Edit:

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');

Output

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

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 ) 

During comparisons chars can be treated as numbers

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

_蜘蛛 2025-01-04 11:33:05

在Java中:Character类有一个名为isLowerCase(Char ch)和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
}

In Java : Character class has static method called isLowerCase(Char ch) ans isUpperCase(Char ch) , Character.isDigit(Char ch)gives you Boolean value, base on that you can easily achieve your task

example:

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
}
初熏 2025-01-04 11:33:05

关于你的代码的一些评论,

  • 为什么你想要有 2 个 for 循环,比如 for(i='A';i<='Z';i++),如果你可以用一个简单的 检查一下>if 语句...您循环整个范围,同时您可以简单地检查它是否包含在该范围内
  • ,即使您找到了答案(例如,当输入为 A 时,您将第一次进入第一个循环时就有结果)您仍然循环所有其余
  • System.out.println("Lowercase"); 语句(和大写语句)被放置在错误的循环中
  • 如果您被允许使用它,我建议查看Character 类,它有很好的 isUpperCaseisLowerCase 方法,

我把剩下的留给你,因为它是家庭作业

Some comments on your code

  • why would you want to have 2 for loops like for(i='A';i<='Z';i++), if you can check this with a simple if statement ... you loop over a whole range while you can simply check whether it is contained in that range
  • even when you found your answer (for example when input is A you will have your result the first time you enter the first loop) you still loop over all the rest
  • your System.out.println("Lowercase"); statement (and the uppercase statement) are placed in the wrong loop
  • If you are allowed to use it, I suggest to look at the Character class which has for example nice isUpperCase and isLowerCase methods

I leave the rest up to you since it is homework

蔚蓝源自深海 2025-01-04 11:33:05

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

You appear to have upper and lowercase back to front. A-Z would be upper, a-z would be lower. While not exactly efficient - with the inverted case exception, I think it should output correctly.

最冷一天 2025-01-04 11:33:05

这可能不是您正在寻找的,但我认为您应该知道执行此操作的真正方法。您可以使用 java.lang.Character 类的 isUpperCase() 来查找有关字符大小写的信息。您可以使用 isDigit() 来区分数字和字母(仅供参考:))。然后,您可以执行toUpperCase(),然后执行元音切换。这将提高您的代码质量。

This may not be what you are looking for but I thought you oughta know the real way to do this. You can use the java.lang.Character class's isUpperCase() to find aout about the case of the character. You can use isDigit() to differentiate between the numbers and letters(This is just FYI :) ). You can then do a toUpperCase() and then do the switch for vowels. This will improve your code quality.

初与友歌 2025-01-04 11:33:05

这是你的作业,所以我假设你需要使用循环和 switch 语句。
没关系,但是为什么你的所有循环都在私有循环的内部?

只要将它们提升到相同的“级别”,您的代码就可以了! (低/高混合的一部分)。

提示:按额外的“Enter”键和“Enter”键。 “空间”是免费的!
(这是我对你的代码做的第一件事,问题变得非常微不足道)

This is your homework, so I assume you NEED to use the loops and switch statments.
That's O.K, but why are all your loops inner to the privious ones ?

Just take them out to the same "level" and your code is just fine! (part to the low/up mixup).

A tip: Pressing extra 'Enter' & 'Space' is free!
(That's the first thing I did to your code, and the problem became very trivial)

2025-01-04 11:33:05
 Char input;

 if (input.matches("^[a-zA-Z]+$")) 
 {
     if (Character.isLowerCase(input))
     {
        // lowercase
     } 
     else
     { 
        // uppercase
     }

     if (input.matches("[^aeiouAEIOU]"))
     {
          // vowel
     }
     else 
     {
         // consonant
     }
 }
 else if (input.matches("^(0|[1-9][0-9]*)$"))
 {
       // number
 }
 else
 {
     // invalid
 }
 Char input;

 if (input.matches("^[a-zA-Z]+$")) 
 {
     if (Character.isLowerCase(input))
     {
        // lowercase
     } 
     else
     { 
        // uppercase
     }

     if (input.matches("[^aeiouAEIOU]"))
     {
          // vowel
     }
     else 
     {
         // consonant
     }
 }
 else if (input.matches("^(0|[1-9][0-9]*)$"))
 {
       // number
 }
 else
 {
     // invalid
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文