所以我有一个方法可以创建一个包含100个随机生成的字符的数组。
这是:
//method to generate random character between ch1 and ch2
public static char getRandomCharacter(char ch1, char ch2)
{
return (char) (ch1 + Math.random() * (ch2 -ch1 +1));
}
//==========================================
//method to assign generated characters (between a and z) to a 100 character array
public static char[] createArray()
{
//declare a 100 character array
char[] character = new char[100];
//for loop assigning the random characters to the array using getRandomCharacter method
for (int x = 0; x < character.length; x++)
character[x] = getRandomCharacter('a', 'z');
//for loop outputting the characters in the array
for (int x = 0; x < character.length; x++)
System.out.println(character[x]);
return character;
}
现在我需要创建一个获取100个生成字符的方法,并计算每个元音生成的次数。我坚持这个。
这是我到目前为止所做的:
public static void countArray()
{
int vowelA, vowelE, vowelI, vowelO, vowelU, vowelY;
int elsePlaceHolder = 0;
for (int x = 0; x < 100; x++)
{
if ((createArray()) == ('a'))
vowelA++;
else if ((createArray()) == ('e'))
vowelE++;
else if ((createArray()) == ('i'))
vowelI++;
else if ((createArray()) == ('o'))
vowelO++;
else if ((createArray()) == ('u'))
vowelU++;
else if ((createArray()) == ('y'))
vowelY++;
else
elsePlaceHolder++;
}
我认为使用for循环执行此操作是正确的,但我认为我没有正确执行它。一旦执行,我可以使用int变量显示元音计数的次数。那个elsePlaceHolder变量就在那里,因为我不知道如何处理else语句。
有人能用countArray()
方法指出我正确的方向吗?非常感谢!
答案 0 :(得分:1)
我认为你应该修改上面的代码!
public static void countArray()
{
int vowelA=0, vowelE=0, vowelI=0, vowelO=0, vowelU=0, vowelY=0;
int elsePlaceHolder = 0;
char [] arr = new char[100];
arr=createArray();
for (int x = 0; x < 100; x++)
{
if (arr[x] == 'a')
vowelA++;
else if (arr[x] == 'e')
vowelE++;
else if (arr[x] == 'i')
vowelI++;
else if (arr[x] == 'o')
vowelO++;
else if (arr[x] == 'u')
vowelU++;
else if (arr[x] == 'y')
vowelY++;
else
elsePlaceHolder++;
}
System.out.print(vowelA+" "+vowelE+" "+vowelI+" "+vowelO+" "+vowelU+" "+vowelY);
}
它会起作用。另外你还没有初始化那些元音迭代器,但你已经增加了它们!
答案 1 :(得分:0)
您将创建一次数组并计算元素数。
char[] chars = createArray();
for(int i = 0; i < chars.length; i++){
if(chars[i] == 'a')
increment number of A's
else if ...
}
而不是创建5个元素,我创建了一个大小为5且a为arr[0]
的数组,e为arr[1]
等。
在您的代码中
int[] arr = new int[5];
for(int i = 0; i < chars.length; i++){
if(chars[i] == 'a')
arr[0]++;
else if(chars[i] == 'e')
arr[1]++;
//etc
}
答案 2 :(得分:0)
嗨,我刚刚更改了你的代码。 希望对你有所帮助。 看看这个!
private static int vowelA=0;
private static int vowelE=0;
private static int vowelI=0;
private static int vowelO=0;
private static int vowelU=0;
private static int vowelY=0;
private static int elsePlaceHolder = 0;
public static void main(String[] args) {
char[] chs = createArray();
System.out.println("vowelA : "+vowelA);
System.out.println("vowelE : "+vowelE);
System.out.println("vowelI : "+vowelI);
System.out.println("vowelO : "+vowelO);
System.out.println("vowelU : "+vowelU);
System.out.println("vowelY : "+vowelY);
}
//method to generate random character between ch1 and ch2
public static char getRandomCharacter(char ch1, char ch2)
{
return (char) (ch1 + Math.random() * (ch2 -ch1 +1));
}
//==========================================
//method to assign generated characters (between a and z) to a 100 character array
public static char[] createArray()
{
//declare a 100 character array
char[] character = new char[100];
//for loop assigning the random characters to the array using getRandomCharacter method
for (int x = 0; x < character.length; x++){
character[x] = getRandomCharacter('a', 'z');
countArray(character[x]);
}
//for loop outputting the characters in the array
for (int x = 0; x < character.length; x++)
System.out.println(character[x]);
return character;
}
public static void countArray(char ch)
{
if (ch == ('a')){
vowelA++;}
else if (ch == 'e'){
vowelE++;}
else if (ch == 'i'){
vowelI++;}
else if (ch == 'o'){
vowelO++;}
else if (ch == 'u'){
vowelU++;}
else if (ch == 'y'){
vowelY++;}
else{
elsePlaceHolder++;}
}
答案 3 :(得分:0)
char[] randomc = new char[]{'a','b','i','i','o','u','u','e','e','e','e','e','e','z','b','f'};
int[] voweltable = new int[] {0, 0, 0, 0, 0, 0}; // Last element is for non-vowels
for (int i = 0; i <= randomc.length - 1; i++)
{
switch(randomc[i])
{
case 'a':
voweltable[0]++;
break;
case 'e':
voweltable[1]++;
break;
case 'i':
voweltable[2]++;
break;
case 'o':
voweltable[3]++;
break;
case 'u':
voweltable[4]++;
break;
default:
voweltable[5]++;
break;
}
}
for (int i = 0; i <= voweltable.length -1; i++)
System.out.println(voweltable[i]);
答案 4 :(得分:0)
与getRandomCharacter()
方法相同,您可以尝试使用字符的int值,并为ch1
和{{之间的范围大小构建一个int数组。 1}},然后增加每次出现的值,然后打印元音的计数,如:
ch2