java设一字符串中包含有大写字母的字符,也有下写字母的字符,编写一程序,将其中的大小写字母的字符分别输出。
时间: 2023-11-22 09:53:42 浏览: 54
你可以使用Java中的isUpperCase和isLowerCase方法来判断字符是否为大写或小写字母,并使用for循环遍历字符串中的每个字符,根据判断结果输出大写或小写字母。下面是示例代码:
```java
public class Main {
public static void main(String[] args) {
String str = "Hello World";
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (Character.isUpperCase(c)) {
System.out.println(c + " is an uppercase letter.");
} else if (Character.isLowerCase(c)) {
System.out.println(c + " is a lowercase letter.");
}
}
}
}
```
输出结果为:
```
H is an uppercase letter.
e is a lowercase letter.
l is a lowercase letter.
l is a lowercase letter.
o is a lowercase letter.
W is an uppercase letter.
o is a lowercase letter.
r is a lowercase letter.
l is a lowercase letter.
d is a lowercase letter.
```
阅读全文