Hey everyone,
I was doing an exercise from Java book and I didn't get how this code works.. I know how the switch statement works. However, I don't understand why they pass a number to the Random class and also to the .nextInt() method on rand. And also, I am not sure where the alphabets are coming from. I'm assuming that the variable c converts the letter a to an integral value and adds 26 to it?
Would anyone be happy to walk me through the code? This will be GREAT.
Thanks, folks.
Random rand = new Random(47);
for(int i=0; i < 100; i++){
int c = rand.nextInt(26) + 'a';
println((char) c + ", " + c + ": ");
switch(c){
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
println("vowel \n");
break;
case 'y':
case 'w':
println("Sometimes a vowel \n");
break;
default: println("consonant!");
}
}