This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
Or: think carefully about what a switch statement does--it can only compare a single value at a time. You, on the other hand, want to compare a range of values. Rob's answer is correct, but I'd rather you took the time to consider *why* the switch statement wasn't working, and why Rob's answer does.
Dmitri Makovetskiy
Ranch Hand
Joined: Jun 21, 2010
Posts: 128
posted
0
David Newton wrote:Or: think carefully about what a switch statement does--it can only compare a single value at a time. You, on the other hand, want to compare a range of values. Rob's answer is correct, but I'd rather you took the time to consider *why* the switch statement wasn't working, and why Rob's answer does.
This exercise is only on the switch function. you are not allowed to use if or while or any other fancy method..
i think i am close to the answer, but something is missing to make the "other" -10 to 50. alternatively , make the input go to 100 and not above
If you have certain requirements for a particular problem, you need to state them up front--how else can we be expected to help in any reasonable way?
So you have at least a couple of options: you could do some match to make the switch statement work without listing all the numbers, or you could just list all the numbers.
Do you have any *other* requirements we need to know?
Dmitri Makovetskiy
Ranch Hand
Joined: Jun 21, 2010
Posts: 128
posted
0
Dmitri Makovetskiy wrote:
David Newton wrote:Or: think carefully about what a switch statement does--it can only compare a single value at a time. You, on the other hand, want to compare a range of values. Rob's answer is correct, but I'd rather you took the time to consider *why* the switch statement wasn't working, and why Rob's answer does.
This exercise is only on the switch function. you are not allowed to use if or while or any other fancy method..
i think i am close to the answer, but something is missing to make the "other" -10 to 50. alternatively , make the input go to 100 and not above
why cant i write before the case 60:
and at the end
default:
System.out.println("invalid number");
Dmitri Makovetskiy
Ranch Hand
Joined: Jun 21, 2010
Posts: 128
posted
0
David no. There is a line that i need to add, i think it should prevent the numbers going over 100.
the way i listed above, is a bit unwanted, cause i was instructed to make 5 cases and not 10...
but if i have no choice, i will have to go with the way above.
is there a way to make the numbers go till 100?
if i put somewhere near the end this statement:
number=100
If you want to do this just with a switch block, you need to specify 100-69 = 31 case items.
If not, you should use some conditions like if-checks or ternary-if-checks.