求JAVA code: 26个字母里随机出现10个字母,其中至少含有两个元音字母(元音字母:a,e, i,o,u)

2个回答

  • 不知道你这10个字母中是否能出现重复的字母!

    import java.util.Random;

    import java.util.ArrayList;

    public class RandomChar

    {

    public char[] Vowel = {'a', 'e', 'i', 'o', 'u'};

    public String[] Consonant = {"b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","y","z"};

    public ArrayList Rusult = new ArrayList();

    public RandomChar()

    {

    }

    public void getChar()

    {

    ArrayList VowelAL = new ArrayList();

    ArrayList ConsonantAL = new ArrayList();

    //VowelAL = genarate(Vowel[]);

    //ConsonantAL = genarate(Consonant[]);

    //下面两个for循环主要输初始化arrayList

    for(int i = 0; i < 21; i++ )

    {

    ConsonantAL.add( Consonant[i] );

    }

    for(int j = 0; j < 5; j++ )

    {

    VowelAL.add( Vowel[j] );

    }

    int randNum01;

    int randNum02;

    int randNum03;

    int randNum04;

    int rangeNum01 = 21;

    int rangeNum02 = 5;

    int ConsonantNum = 8;

    int VowelNum = 2;//这个是代表输出8个辅音的同时输出元音的个数

    for(int i = 0; i< ConsonantNum; i++)

    {

    Random ran = new Random(System.currentTimeMillis()+i);

    randNum01 = ran.nextInt(rangeNum01);

    randNum02 = ran.nextInt(2);

    //randNum02主要是来控制是否输出元音,

    //这个数也是随机的因此在结果集中的位置就是随机的了

    if(randNum02 == 1 && VowelNum !=0)

    {

    randNum03 = ran.nextInt(rangeNum02);

    System.out.print(VowelAL.get(randNum03));

    Rusult.add(VowelAL.get(randNum03));

    VowelAL.remove(randNum03);//通过remove语句将输出过的字符删除

    VowelNum = VowelNum - 1;//元音已输出个数减一

    rangeNum02 = rangeNum02 - 1;//同样随机数的范围减一

    }

    System.out.print(ConsonantAL.get(randNum01));

    Rusult.add(ConsonantAL.get(randNum01));

    ConsonantAL.remove(randNum01);//通过remove语句将输出过的字符删除

    rangeNum01 = rangeNum01 -1;//同样随机数的范围减一

    }

    //判断是否已经输出2个字符,没有则在后面补充输入;这样的情况同样是随机的

    if (VowelNum != 0)

    {

    for (int k = 0; k < VowelNum; k++)

    {

    Random ran = new Random(System.currentTimeMillis());

    randNum04 = ran.nextInt(rangeNum02);

    System.out.print(VowelAL.get(randNum04));

    Rusult.add(VowelAL.get(randNum04));

    VowelAL.remove(randNum04);

    rangeNum02 = rangeNum02 -1;

    }

    }

    }

    public void PrintRes()

    {

    for(int i = 0; i < 10 ; i++)

    {

    System.out.println(Rusult.get(i));

    }

    }

    public static void main(String str[])

    {

    RandomChar rand = new RandomChar();

    System.out.print("下面是getChar函数中的print输出的字符:");

    rand.getChar();

    System.out.println();

    System.out.println("下面是PrintRes函数中通过存储的Arraylist输出的字符:");

    rand.PrintRes();

    }

    }