java用一个数组存储26个英文字母的小写形式a-z (2)遍历显示小写字母以及它对应的大写字母,例如:a->A,b->B,c->C等
时间: 2024-03-05 18:55:06 浏览: 19
好的,针对你的问题,我可以给出以下代码示例:
```java
public class AlphabetArray {
public static void main(String[] args) {
char[] lowercase = new char[26];
for (int i = 0; i < 26; i++) {
lowercase[i] = (char) ('a' + i);
}
for (int i = 0; i < 26; i++) {
System.out.println(lowercase[i] + "->" + Character.toUpperCase(lowercase[i]));
}
}
}
```
这段代码中,我们首先创建了一个长度为26的字符数组 `lowercase`,并使用循环语句将字母 `a` 到 `z` 存储到数组中。然后,再次使用循环语句遍历数组中的每个字母,并使用 `Character.toUpperCase()` 方法将其转换为大写字母进行显示。运行代码,输出结果如下:
```
a->A
b->B
c->C
d->D
e->E
f->F
g->G
h->H
i->I
j->J
k->K
l->L
m->M
n->N
o->O
p->P
q->Q
r->R
s->S
t->T
u->U
v->V
w->W
x->X
y->Y
z->Z
```
希望这个代码示例可以帮助到你!
相关问题
编写一个java 程序,输出一个字符串中的大写英文字母数、小写英文字母数以及非英文字母数。
可以按照以下代码来实现:
```java
public class CountLetters {
public static void main(String[] args) {
String str = "Hello, World! 123";
int upperCaseCount = 0;
int lowerCaseCount = 0;
int nonLetterCount = 0;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch >= 'A' && ch <= 'Z') {
upperCaseCount++;
} else if (ch >= 'a' && ch <= 'z') {
lowerCaseCount++;
} else {
nonLetterCount++;
}
}
System.out.println("大写字母数: " + upperCaseCount);
System.out.println("小写字母数: " + lowerCaseCount);
System.out.println("非字母数: " + nonLetterCount);
}
}
```
输出结果为:
```
大写字母数: 2
小写字母数: 10
非字母数: 8
```
这个程序使用了一个 for 循环遍历字符串中的每一个字符,然后根据字符的 ASCII 码值判断它是大写字母、小写字母还是非字母。最后统计出各种字符的数量并输出。
C语言用数组输入一行字符串,统计其中大写字母、小写字母、空 洛以及数字个数。
好的,您的问题是关于 C 语言中如何统计输入字符串中大写字母、小写字母、空格和数字的个数。可以通过以下代码实现:
```c
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char str[100];
int len, i, upper=0, lower=0, space=0, digit=0;
printf("请输入一行字符串:\n");
fgets(str, 100, stdin);
len = strlen(str);
for(i=0; i<len; i++) {
if(isupper(str[i]))
upper++;
else if(islower(str[i]))
lower++;
else if(isspace(str[i]))
space++;
else if(isdigit(str[i]))
digit++;
}
printf("大写字母个数:%d\n", upper);
printf("小写字母个数:%d\n", lower);
printf("空格个数:%d\n", space);
printf("数字个数:%d\n", digit);
return 0;
}
```
首先通过 `fgets` 函数读取用户输入的字符串,并使用 `strlen` 函数获取字符串的长度。然后使用一个循环遍历字符串中的每个字符,使用 `isupper`、`islower`、`isspace` 和 `isdigit` 函数判断字符的类别,并统计对应类别的字符个数。最后输出结果即可。