0

At last!
You have a switch with cases for 1,2,3,4 and 5. You enter 70, which is not 1,2,3,4 or 5, so it executes the default. That's how switch is supposed to work.

0

OK so apart from IF else ... statement cant any other condition be used?

So how am I suppose to use switch in this case..
thats what I want to do

Edited by gh.fuz

0

No. If you want to test for numbers being in particular ranges you need if/elseif/else. Switch only works for specific values, not for ranges.

0

ok then I'll need to use that instead

thanks a lot for your guide
am still learning so kindly take it easy with me

appreciated
regarda

0

or, as suggested earlier:

switch(score){
case 60:
case 61:
case ...
case 69: print statement; break; // the above will execute everything until the first break (this one) they encounter
...
}
0

or more likely, you're going to want to have a method to determine the grade based on the score. A switch would be rather impractical...
Something like

if (score < 11) { 
  result = "A";
} else if (score < 21) {
  result = "B";
} else // ... etc ...

would be much more efficient.

0

... or, while we're having fun...
for the simplest possible code and easiest maintenance, build a TreeMap with the upper bound of each range as key and the grade as value, then use ceilingEntry, eg

     TreeMap<Integer, String> map = new TreeMap<>();
     map.put(10,"A");
     map.put(20,"B");
     map.put(30,"C");

    int score = 15;

    String grade = map.ceilingEntry(score).getValue();

    System.out.println(score + " = " + grade);
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.