Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi, I'm a beginning programmer and I have an assignment where I am using eclipse and have to write a program that produces 3 random capital vowels...like a slotmachine. I was wondering if I can get a little help because I'm a little lost. Thanks
RRRRR OOOOO BBBBB

What do you have so far? Have you looked into Randomizing examples online? Have you attempted to begin coding?
IR

well I know how to write code that will produce any random letter but i dont know how to produce just random vowels....I'm not sure what variables need to be set???
RRRRR OOOOO BBBBB

try
char c = 'random letter';
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'){
codeeee...
codeee...
}I don't remeber but I think that you should use an equal method instead of de ==
Hope it works
iL Huevo

Or what you could do is construct an array of vowels if you've learned this already. Then, you can get a random number between 0 and 4 and use it to determine which of the vowels in the array should pop up.
For example:
char[] vowels = new char[5];
vowels[0] = 'a';
vowels[1] = 'e';
vowels[2] = 'i';
vowels[3] = 'o';
vowels[4] = 'u';
Random r = new Random();And each time you want to get a random vowel:
int x = r.nextInt(5); //Brings up an integer between 0 and 4, inclusivechar thevowel = vowels[x];
And thevowel will have your vowel. Of course, if your class doesn't use char, you can always use String as well. Just replace char with String and you should be fine.
But if you haven't learned about arrays/arraylists/stuff like that, go with what iL_Huevo says. For char, == is right because it's a primitive data type. But for objects like strings, you should use equals() instead.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |