Forums Register Login

Another switch statement query

+Pie Number of slices to send: Send
Hi everyone,

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?

Thanks again!!
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

Joanne Neal wrote:

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......
+Pie Number of slices to send: Send
As it was pre-coffee I think we can forgive you
+Pie Number of slices to send: Send
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:
+Pie Number of slices to send: Send
Ok, but if I were to put a System.out.println() after every case, it would output all of them starting with the matching case condition, right?
+Pie Number of slices to send: Send
Hi everyone, thanks for all the help.

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
+Pie Number of slices to send: Send
@Joe: Sure, it'd print them--but that doesn't seem to have much to do with what the code is trying to accomplish, no?
+Pie Number of slices to send: Send
Your right, not really no. I'm sorry I can't explain myself better.

Basically, I want one of those values to be returned, so it can be added to a cost which I have already calculated.
+Pie Number of slices to send: Send
I was replying to Joe; sorry.

double foo = chooseGrade(theGrade)

Then just use foo.
+Pie Number of slices to send: Send
Hi my bad, I just realised. Thanks for that
It's exactly the same and completely different as this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1080 times.
Similar Threads
help: java program to convert decimal number to hexadecimal number
end of file indicator for more than one file
using user input to pick Object to be used in case/Switch
Stacks
Parsing Commands from String Input
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 03:59:34.