Welcome to Omgili,
Omgili ( Oh My God I Love It ;) is a search engine for discussions. With Omgili you can find answers and solutions, debates, discussions, personal experiences, opinions and more... To learn more about Omgili click here.
This is a complete preview of the discussion as it was indexed by Omgili crawlers. Use this preview if the original discussion is unavailable.
Click here to view the original discussion.
[http://forum.java.sun.com/thread.jspa?t...]
Click here to search for discussions with Omgili discussions search engine.
 |
New To Java - switch case, greater than AND smaller than. possible???
Hallo Mates.
I'm a newbie for Java and this is my first post.
I have some difficulty, looking for the best solution for my homework but don't know how to apply it:
1.
Supposed i have an integer 'points' and I want to determine a value for 'note' to each value of 'points' under the following key:
for 'points' with a value of 90+: note=1
for 'points' with a value of 80-89 : note=2
for 'points' with a value of 70-79 (including 60): note=3
for 'points' with a value of 60-69 (including 60): note=4
for 'points' with a value of less then 60: note=5
and i want to use the switch-case stucture.
Is there a way to solve it with 5 cases, or do i have to write a case for each possible amount of points?
something like
switch (note);
case =>90 : note=1;
Break;
would produce an error, so how do i do it ?
Greetz and many thanks,
Amit.
|
 |
|
What you want is if and else statements.
Read about if/else here: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/if.html
But as your new to java I suggest you also read about switch case statements as its good knowledge as well.
Read about switch case here: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/switch.html
|
 |
|
Thank you!
|
 |
|
I know if and then
and i think it would be also the best solution:
if points<60 then note=5
if points=>60 then note=4
if points=>70 then note=3
if points=>80 then note=2
if points=>90 then note=1
but, is there a way to do the same with switch-case without using one million different cases?
|
 |
|
No.
|
 |
Switch (points/10) { ...
}
???
|
 |
You might want to make use of the else block to reduce the actual amount of processing:
if ( points <
60 ) { note = 5; } else if ( points <
70 ) { note = 4; } else if ( points <
80 ) { note = 3; } else if ( points <
90 ) { note = 2; } else { note = 1;
}
There would be ways of sticking certain rules into a collection and then using that to construct your translation function, but unless the translation of points to note is going to change at run-time, or there are a lot of different rules, there's really no reason to do anything other than a set of ifs.
|
 |
If 87points then?
Or 89, or 111?
|
 |
It's not really what the switch structure is for.
I wouldn't do it like that if I were you;
In 6 months time you won't have the faintest idea what the code does!
|
 |
Quote: :
If 87points then?
Or 89, or 111? If what?
Try it yourself! The code works, but as dcminter added, it's not a visually intuitive way of doing it.
It happens to work in this case.
|
 |
|
-.- I was refering to BigDaddyLoveHandles reply
Not my fault you replied 5sec before me..
|
 |
|
Quote: :
-.- I was refering to BigDaddyLoveHandles reply
Not my fault you replied 5sec before me..
I meant my "switch (points/10)" humorously, but it will map 80, 81, 82, 83, 84, 85, 86, 87, 88 and 89 all to case 8.
|
 |
Quote: :
-.- I was refering to BigDaddyLoveHandles reply
Not my fault you replied 5sec before me..
Yes, and I was replying to you referring to BigDaddy's reply.
His solution works fine, as he notes above.
And you can avoid mixups (even though this one wasn't) by quoting.
|
 |
Quote: :
Quote: :
-.- I was refering to BigDaddyLoveHandles reply
Not my fault you replied 5sec before me..
Yes, and I was replying to you referring to BigDaddy's reply.
His solution works fine, as he notes above.
And you can avoid mixups (even though this one wasn't) by quoting.
Fine if you wanna continue an dead subject with useless, Im holier than you ****.
Then enlighten me with how you will make diffrense to 82 and 87 for example which both will be pointed to the case 8: in this example.
Ofcouse it doesnt matter here.
But ffs. this is not the only example in the world.
|
 |
Quote: :
Fine if you wanna continue an dead subject with useless, Im holier than you ****.
I wasn't continuing a dead thread.
You hadn't yet got it through your head that BDLH's solution worked in this case.
Then enlighten me with how you will make diffrense to 82 and 87 for example which both will be pointed to the case 8: in this example.
I won't make the differentiation.
The example does not require me to.
Ofcouse it doesnt matter here.
But ffs. this is not the only example in the world.
It was the only example on this thread, and it worked.
Not to mention that both the OP and other posters had agreed that ifs were the best solution BEFORE then, and the OP had still inquired into the possibility of it being done with switches.
At that point, it's quite obvious nobody was looking for a generalized solution with switches, no?
|
|
|
|