Question:
I am not understanding this switch case Java little program?
William
2013-03-16 07:10:21 UTC
Can somebody explain me what this program does? I hate when they are using " random" as example. I normally understand the case switch statement but not in this case with the random class :( Thanks.


class TheOldSwitcheroo {
public static void main(String args[]) {
Scanner myScanner = new Scanner(System.in);
Random myRandom = new Random();
int randomNumber;
out.print(“Type your question, my child: “);
myScanner.nextLine();
randomNumber = myRandom.nextInt(10) + 1;
switch (randomNumber) {
case 1:
out.println(“Yes. Isn’t it obvious?”);
break;
case 2:
out.println(“No, and don’t ask again.”);
break;
case 3:
out.print(“Yessir, yessir!”);
out.println(“ Three bags full.”);
break;
case 4:
out.print(“What part of ‘no’”);
out.println(“ don’t you understand?”);
break;
case 5:
out.println(“No chance, Lance.”);
break;
case 6:
out.println(“Sure, whatever.”);
break;
case 7:
out.print(“Yes, but only if”);
out.println(“ you’re nice to me.”);
break;
case 8:
out.println(“Yes (as if I care).”);
break;
case 9:
out.print(“No, not until”);
out.println(“ Cromwell seizes Dover.”);
break;
case 10:
out.print(“No, not until”);
out.println(“ Nell squeezes Rover.”);
break;
default:
out.print(“You think you have”);
out.print(“ problems?”);
out.print(“ My random number”);
out.println(“ generator is broken!”);
break;
}
out.println(“Goodbye”);
}
}
Four answers:
peteams
2013-03-16 10:04:14 UTC
The program asks a question and then gives a random answer to the question.



The question is asked by these lines:



Scanner myScanner = new Scanner(System.in);

out.print(“Type your question, my child: “);

myScanner.nextLine();



These lines generate a random number between 1 and 10 inclusive; the call to nextInt(10) gives a number between 0 and 9 inclusive that is incremented by 1.



Random myRandom = new Random();

int randomNumber;

randomNumber = myRandom.nextInt(10) + 1;



The switch-statement then prints one of ten potted responses, for example "Yes, isn't it obvious?" if nextInt(10) return 0, and hence randomNumber ended up as 1.



switch (randomNumber) {

case 1:

out.println(“Yes. Isn’t it obvious?”);

break;



The default case cannot be reached, but it is often a good idea to have a default clause.
green meklar
2013-03-16 22:51:19 UTC
The Random.nextInt(n) method gives you an integer from 0 to n-1 inclusive. Adding 1 bumps that up to 0 to n inclusive, so in this case, the range 1 - 10.



In this code, the random 1 - 10 value is then stored and used as the input for the switch. Since each case ends with its own break statement, effectively a random case will be chosen and run. So 1/10 of the time it will say 'No, and don’t ask again.', 1/10 of the time it will say 'Sure, whatever.', and so on.



Because all the case numbers from 1 to 10 are included, the default case should never be reached, hence the message there about the broken RNG.
Ratchetr
2013-03-16 08:18:54 UTC
The default case here is right.



The largest number nextInt(10) can return is 9, not 10. From the Java spec:

"Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive)..."
magnes
2016-12-01 10:33:20 UTC
The ruin; fact kicks you out of the change fact (it "breaks" out of the change). in case you needed it to proceed to case 2, you does no longer could do something specific different than no longer ruin. As a programmer, sure you will use this. It has its specific makes use of and seems plenty cleanser then an prolonged record of if/else statements.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...