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.
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
}
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
您的大小写似乎是从后到前的。 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.
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.
发布评论
评论(8)
如果不是作业,您可以使用现有方法,例如
Character.isDigit(char)
、Character.isUpperCase(char)
和Character.isLowerCase( char)
有点“聪明”,因为它们不仅可以在 ASCII 中运行,还可以在各种字符集中运行。If it weren't a homework, you could use existing methods such as
Character.isDigit(char)
,Character.isUpperCase(char)
andCharacter.isLowerCase(char)
which are a bit "smarter", because they don't operate only in ASCII, but also in various charsets.您的代码中不需要 for 循环。
以下是重新实现方法的方法
编辑:
这里是提示您继续,以下代码片段给出了
char的
int
值输出
以下是如何
在比较期间检查两个数字(例如
a
和b
)之间是否存在数字x
< code>chars 可以被视为数字如果你能结合这些提示,你应该能够找到你想要的;)
You don't need a for loop in your code.
Here is how you can re implement your method
Edit:
Here is hint for you to proceed, Following code snippet gives
int
values forchar
sOutput
Here is how you can check if a number
x
exists between two numbers saya
andb
During comparisons
char
s can be treated as numbersIf you can combine these hints, you should be able to find what you want ;)
在Java中:Character类有一个名为isLowerCase(Char ch)和isUpperCase(Char ch)的静态方法,Character.isDigit(Char ch)给你布尔值,基于此你可以轻松实现你的任务
示例:
String abc =“HomePage” ;
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";
关于你的代码的一些评论,
for(i='A';i<='Z';i++)
,如果你可以用一个简单的检查一下>if
语句...您循环整个范围,同时您可以简单地检查它是否包含在该范围内A
时,您将第一次进入第一个循环时就有结果)您仍然循环所有其余System.out.println("Lowercase");
语句(和大写语句)被放置在错误的循环中Character
类,它有很好的isUpperCase
和isLowerCase
方法,我把剩下的留给你,因为它是家庭作业
Some comments on your code
for(i='A';i<='Z';i++)
, if you can check this with a simpleif
statement ... you loop over a whole range while you can simply check whether it is contained in that rangeA
you will have your result the first time you enter the first loop) you still loop over all the restSystem.out.println("Lowercase");
statement (and the uppercase statement) are placed in the wrong loopCharacter
class which has for example niceisUpperCase
andisLowerCase
methodsI leave the rest up to you since it is homework
您的大小写似乎是从后到前的。 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.
这可能不是您正在寻找的,但我认为您应该知道执行此操作的真正方法。您可以使用 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'sisUpperCase()
to find aout about the case of the character. You can useisDigit()
to differentiate between the numbers and letters(This is just FYI :) ). You can then do atoUpperCase()
and then do the switch for vowels. This will improve your code quality.这是你的作业,所以我假设你需要使用循环和 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)