Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# Something is missing in my Switch method

Dmitri Makovetskiy
Ranch Hand
Posts: 128

the instruction to this code is to make the user write numbers from 1-100 and the application should categorize the numbers in the following way

100 - A
90-99 - B
80-89 - C
70-79 - D
60-69 - E
other - F

the problem is with the other. it should go from 59 to 0.

however, the application categorizes any number as "others"

how can i cause the application to accept numbers from 0-100 and not accept numbers higher than 100? [/code]

Rob Spoor
Sheriff
Posts: 20532
54
Use a series of if-statements:

David Newton
Author
Rancher
Posts: 12617
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
Posts: 128
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

David Newton
Author
Rancher
Posts: 12617
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
Posts: 128
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
Posts: 128
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

would it work?

David Newton
Author
Rancher
Posts: 12617
You need to check the number for validity *before* entering the switch statement.

Devaka Cooray
ExamLab Creator
Marshal
Posts: 4435
256
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.

David Newton
Author
Rancher
Posts: 12617
(@Devaka: not necessarily--check the math in the original post.)

Devaka Cooray
ExamLab Creator
Marshal
Posts: 4435
256
David Newton wrote:(@Devaka: not necessarily--check the math in the original post.)

Oh ya, thanks!