编写一个程序,设定一个有大小写字母的字符串,先将字符串的大写字符输出,再将字符串中的小写字符输出。(用Java实现
时间: 2024-05-08 10:18:11 浏览: 36
)好的,这是一个比较简单的程序,以下是Java代码:
```
public class StringOutput {
public static void main(String[] args) {
String str = "HelloWorld";
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (Character.isUpperCase(c)) {
System.out.print(c);
}
}
System.out.println();
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (Character.isLowerCase(c)) {
System.out.print(c);
}
}
}
}
```
这个程序的实现比较简单,主要是使用了Java中的字符串和字符相关的API。首先定义了一个字符串变量str,然后使用for循环遍历字符串中的每一个字符,通过Character.isUpperCase方法判断字符是否为大写字母,如果是则输出。同样的方法也可以输出小写字母。
相关问题
编写一个JAVA程序,设定一个有大小写字母的字符串,先将字符串的大写字符输出,再将字符串中的小写字符输出。
您好,我可以回答这个问题。以下是JAVA程序的代码:
```
public class StringCase {
public static void main(String[] args) {
String str = "HelloWorld";
for (int i = ; i < str.length(); i++) {
char ch = str.charAt(i);
if (Character.isUpperCase(ch)) {
System.out.print(ch);
}
}
for (int i = ; i < str.length(); i++) {
char ch = str.charAt(i);
if (Character.isLowerCase(ch)) {
System.out.print(ch);
}
}
}
}
```
这个程序会先输出字符串中的大写字符,再输出小写字符。
我还想用c#实现设定一个有大小写字母的字符串,先将字符串的大写字母输出,再将字符串的小写字母输出。
好的,以下是用C#实现设定一个有大小写字母的字符串,先将字符串的大写字母输出,再将字符串的小写字母输出的代码:
```csharp
using System;
namespace StringToUpperLower
{
class Program
{
static void Main(string[] args)
{
string str = "HeLLoWoRlD";//设定一个有大小写字母的字符串
Console.WriteLine("原字符串:" + str);
Console.Write("大写字母:");
for (int i = 0; i < str.Length; i++)
{
if (Char.IsUpper(str[i]))//判断字符是否为大写字母
{
Console.Write(str[i] + " ");//输出大写字母
}
}
Console.Write("\n小写字母:");
for (int i = 0; i < str.Length; i++)
{
if (Char.IsLower(str[i]))//判断字符是否为小写字母
{
Console.Write(str[i] + " ");//输出小写字母
}
}
Console.ReadKey();
}
}
}
```
运行程序后,会输出原字符串,再分别输出大写字母和小写字母。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)