java编程:用26个英文字母随即产生1000个英文大写字母,选出元音字母的个数以降序排列并输出相应的个数

发布网友 发布时间:2022-04-23 03:33

我来回答

4个回答

热心网友 时间:2023-10-13 11:18

public class Test {
public static void main(String[] args) {
String[] arr = new String[]{"a","b","c","d","e","f","g","h","i","j","k",
"l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
List<String> list=new ArrayList<String>();
Random rnd = new Random();
int countA = 0;
int countE = 0;
int countI = 0;
int countO = 0;
int countU = 0;
for (int i = 0; i < 1000; i++) {
int index=rnd.nextInt(26);
String temp=arr[index];
if ("a".equals(temp) || "e".equals(temp) || "i".equals(temp)
|| "o".equals(temp) || "u".equals(temp)) {
list.add(temp) ;
}
if ("a".equals(temp)) {
countA++;
} else if ("e".equals(temp)) {
countE++;
} else if ("i".equals(temp)) {
countI++;
} else if ("o".equals(temp)) {
countO++;
} else if ("u".equals(temp)) {
countU++;
}
}
Collections.sort(list,new Comparator() {
public int compare(Object o1, Object o2) {
String s1 = (String)o1;
String s2 = (String)o2;

return s1.compareTo(s2)>0 ? 1 :-1;
}
});

System.out.println(list);
System.out.println("a:"+countA+",e:"+countE+",i:"+countI+",o:"+countO+",u:"+countU);
}
}追问得到元音字母后,还要以随机的元音字母的个数降序排列

追答import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class Test {
public static void main(String[] args) {
String[] arr = new String[]{"a","b","c","d","e","f","g","h","i","j","k",
"l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
List list=new ArrayList();
Random rnd = new Random();
int countA = 0;
int countE = 0;
int countI = 0;
int countO = 0;
int countU = 0;
for (int i = 0; i < 1000; i++) {
int index=rnd.nextInt(26);
String temp=arr[index];
if ("a".equals(temp) || "e".equals(temp) || "i".equals(temp)
|| "o".equals(temp) || "u".equals(temp)) {
list.add(temp) ;
}
if ("a".equals(temp)) {
countA++;
} else if ("e".equals(temp)) {
countE++;
} else if ("i".equals(temp)) {
countI++;
} else if ("o".equals(temp)) {
countO++;
} else if ("u".equals(temp)) {
countU++;
}
}
String[] arr2=new String[]{countA+"a",countE+"e",countI+"i",countO+"o",countU+"u"};
Arrays.sort(arr2);
System.out.println(list);
for (int i = 0; i < arr2.length; i++) {
int index=arr2.length-i-1;
System.out.println(arr2[index].substring(arr2[index].length()-1)+":"+arr2[index].substring(0,arr2[index].length()-1));
}
}
}

热心网友 时间:2023-10-13 11:18

Random rand=new Random();
int j=65;//起始字母
int shu[]=new int[1000];//保存生成的字母
int shao[]=new int[26];//保存字母数量
for(int i=0;i<1000;i++){//生成字母,并计算数量
int s=(int)(rand.nextInt(26));
shu[i]=j+s;
shao[s]++;//计算数量
}
for(int i=0;i<999;i++){//降序排列生成的字母
int zhong=0;
for(int k=0;k<=999-i;k++){
if(shu[i]<shu[k+i]){
zhong=shu[i];
shu[i]=shu[k+i];
shu[k+i]=zhong;
}
}
}

//以上为主要代码,以下为输出代码
for(int i=0;i<1000;i++){//输出降序的字母
System.out.println((char)shu[i]);
}
for(int i=0;i<26;i++){//输出字母的数量
System.out.println((char)(j+i)+"的数量为:"+shao[i]);
}
for(int i=0;i<26;i++){//输出元音字母的数量
if(String.valueOf((char)(j+i)).equals("A")||String.valueOf((char)(j+i)).equals("E")
||String.valueOf((char)(j+i)).equals("I")||String.valueOf((char)(j+i)).equals("O")
||String.valueOf((char)(j+i)).equals("U")){
System.out.println("元音字母"+(char)(j+i)+"的数量为:"+shao[i]);
}
}

热心网友 时间:2023-10-13 11:19

package example;

import java.util.Random;

public class Test {
char[] chs={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
Random r=new Random();
private static String letters = "";
private int a=0;
private int e=0;
private int i=0;
private int o=0;
private int u=0;
//随机产生1000个英文字母
public void Letter(){
for(int i=0;i<1000;i++){
letters+=chs[r.nextInt(chs.length)];
}
System.out.println(letters);
}
//选取元音字母及个数
public void Selection(){
for(int j=0;j<letters.length();j++){
if( letters.charAt(j)=='A'){
a++;
}
if( letters.charAt(j)=='E'){
e++;
}
if( letters.charAt(j)=='I'){
i++;
}
if( letters.charAt(j)=='O'){
o++;
}
if( letters.charAt(j)=='U'){
u++;
}
}
System.out.println("A:"+a+"\nE:"+e+"\nI:"+i+"\nO:"+o+"\nU:"+u);
}

public static void main(String[] args){
new Test().Letter();
new Test().Selection();
}
}

热心网友 时间:2023-10-13 11:19

package example;

import java.util.Random;

public class Test {
char[] chs = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
Random r=new Random();
private static String letters = "";
private int a=0;
private int e=0;
private int i=0;
private int o=0;
private int u=0;
//随机产生1000个英文字母
public void Letter(){
for(int i=0;i<1000;i++){
letters+=chs[r.nextInt(chs.length)];
}
System.out.println(letters);
}
//选取元音字母及个数
public void Selection(){
for(int j=0;j<letters.length();j++){
if( letters.charAt(j)=='A'){
a++;
}
if( letters.charAt(j)=='E'){
e++;
}
if( letters.charAt(j)=='I'){
i++;
}
if( letters.charAt(j)=='O'){
o++;
}
if( letters.charAt(j)=='U'){
u++;
}
}
System.out.println("A:"+a+"\nE:"+e+"\nI:"+i+"\nO:"+o+"\nU:"+u);
}

public static void main(String[] args){
new Test().Letter();
new Test().Selection();
}
}

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com