I have already posted concerning switch statements. I was wondering if someone would be able to tell me how to implement and output a switch statement which I have in one class, say Numbers.
And then use these case inputs in another separate class, using a scanner to take the input and output the values of the case (e.g. input 1 into scanner --> 0.1 returned)
Also, once this has been done, would it be possible to refer back to this value, and use it to add to another number which had been inputted into the scanner?
To get the next number from the scanner I think there is a method like nextInt(). I'd recommend looking at the online JavaDocs for the Scanner class.
And in between each of your case statements you will want to place a break; statement. If you don't it will return every value after the first matching value. So if your case was 1, it would return 0.1, 0.2, 0.2, and 0.00.
W. Joe Smith wrote:And in between each of your case statements you will want to place a break; statement. If you don't it will return every value after the first matching value. So if your case was 1, it would return 0.1, 0.2, 0.2, and 0.00.
No it won't. The return statement will cause the method to exit at that point. If you put a break after the return you will probably get an unreachable statement compilation error.
W. Joe Smith wrote:And in between each of your case statements you will want to place a break; statement. If you don't it will return every value after the first matching value. So if your case was 1, it would return 0.1, 0.2, 0.2, and 0.00.
No it won't. The return statement will cause the method to exit at that point. If you put a break after the return you will probably get an unreachable statement compilation error.
Teach me to try to give advice without coffee...........I didn't even notice the return in there......
And it wouldn't return "every value" anyway--it would return the last one; Java has no native multiple return value capability. Without breaks (or returns) it simply "falls through" to the next case:
Could someone explain how I could pass the output of the case statement to another class which is using a scanner to get the input of either 1, 2 ,3 (e.g, the user inputs 1 into the scanner, then the value 0.10 is returned.)
I cannot seem to work it out how to get the scanner to take the value, then return the corresponding case return. I hope I have been clear enough, Thanks