Why can I not return value in case statement java
Why can I not return value in case statement java
I am making a simple program that generates a random maths question using random numbers between 1 and 10. The operator will also be random between +,- and *. When I try to use a case statement and return the operation value and print the question ( in the end) it says there is no operation variable.
int number1 = (int)(Math.random()* 10) + 1;
int number2 = (int)(Math.random()* 10) + 1;
int operator = (int)(Math.random()* 3) + 1;
switch (operator)
case 1:
String operation = "+";
int correctResult = number1 + number2;
break;
case 2:
String operation = "-";
int correctResult = number1 - number2;
break;
case 3:
String operation = "*";
int correctResult = number1 * number2;
break;
System.out.print(number1+operation+number2+": ");
String studentAnswer = scanner.next();
operation
How do I fix it, I tried to put return values in each case which still doesnt work
– Harry Potter424
Sep 9 '18 at 20:09
See my updated comment. And google for "variable scope"
– JB Nizet
Sep 9 '18 at 20:10
4 Answers
4
You need to declare operation outside od switch block:
int number1 = (int)(Math.random()* 10) + 1;
int number2 = (int)(Math.random()* 10) + 1;
int operator = (int)(Math.random()* 3) + 1;
String operation = null; // move outside of switch block
int correctResult; // move outside of switch block
switch (operator)
case 1:
operation = "+";
correctResult = number1 + number2;
break;
case 2:
operation = "-";
correctResult = number1 - number2;
break;
case 3:
operation = "*";
correctResult = number1 * number2;
break;
System.out.print(number1+operation+number2+": ");
String studentAnswer = scanner.next();
Declare param outside and set in switch case. So this code will be like this;
int number1 = (int) (Math.random() * 10) + 1;
int number2 = (int) (Math.random() * 10) + 1;
int operator = (int) (Math.random() * 3) + 1;
//initalize value which is changing in swich case statement and set initializing value
String operation = "";
int correctResult = 0;
switch (operator)
case 1:
operation = "+";
correctResult = number1 + number2;
break;
case 2:
operation = "-";
correctResult = number1 - number2;
break;
case 3:
operation = "*";
correctResult = number1 * number2;
break;
System.out.print(number1 + operation + number2 + ": ");
String studentAnswer = scanner.next();
The problem is that you are not following the variable visibility scope. You aways need to count the brackets Here is a generic example.
public void exampleScopeMethod
String localVariable = "I am a local Variable";
String nextScope = " NextScope is One level deeper";
localVariable += nextScope
String anotherScope = "Is one level deeper than local variable, still different scope than nextScope";
//Ooops nextScope is no longer visible I can not do that!!!
anotherScope +=nextScope;
one more scope , useless but valid
//Ooopppsss... this is again not valid
return nextScope;
// now it is valid
return localVariable;
}
As JB Nizet said above:
Because the operation variables are defined inside each case block, and are thus not visible outside of these blocks. Declare one variable, before the switch, and modify its value inside the case blocks.
the operation variable is only defined inside each case.
operation
@zx485 The question says, "Why can I not return value in case statement java" ("Why can't I return a value in a case statement in Java?". I answered why.
– Solomon Ucko
Sep 9 '18 at 20:56
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Because the
operationvariables are defined inside each case block, and are thus not visible outside of these blocks. Declare one variable, before the switch, and modify its value inside the case blocks.– JB Nizet
Sep 9 '18 at 20:09