...然后统计产生了多少个元音字 母,并输出这个数字。
发布网友
发布时间:2024-10-24 12:51
我来回答
共2个回答
热心网友
时间:3小时前
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("请输入一个整数:");
int num = in.nextInt();
int count = 0;
String str = "";
String string = "";
for (int i = 0; i < num; i++) {
char ch;
if (Math.random() < 0.5) {
ch = (char) (Math.random() * 26 + 'A');
} else {
ch = (char) (Math.random() * 26 + 'a');
}
str += ch;
if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U'
|| ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
count++;
string += ch;
}
}
System.out.println("输出的英文字母是:" + str);
System.out.println("元音字母的个数:" + count);
System.out.println("元音字母分别是:" + string);
}
}
以上代码可以实现,增加了小写字母的判断,希望可以帮到你
热心网友
时间:3小时前
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.println("请输入:");
int temp = Integer.parseInt(br.readLine());
int res = 0;
for (int i = 0; i < temp; i++) {
char ch = (char) (Math.random() * 26 + 'A');
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U'){
res ++;
System.out.println(ch);
}
}
System.out.println("元音字母的个数:"+res);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}