Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>OK, my assumption is that you will enter a numeric grade 0-100, and that the letter grades you want go as "90-100: A; 80-89: B; ...; 60-69: D; 0-59: F". </p> <p>If that is not the case, your first lesson to be learned about programming is: <strong>the most important part of coding is clearly writing out specifications and requirements</strong></p> <p>Having said that, there are smart and dumb approaches.</p> <ul> <li><p>Dumb approach:</p> <ul> <li><p>Use a switch on the "input" as you do now</p></li> <li><p>Since values 90, 91... 100 are all supposed to be treated the same, put them all in as separate switch cases <strong>but only do the work in the last one</strong> (known as fall-through; <a href="http://java.sun.com/docs/books/tutorial/java/nutsandbolts/switch.html" rel="noreferrer">see SwitchDemo2 on this page for how that works</a>)</p></li> <li><p>For the last value (say, 90 if you start from 100 and go down), the switch statement would be letterGrade = "A"; break; </p></li> <li><p>once you reach case of 89, you repeat the same logic as you did for 100 through 90</p></li> <li><p>Lather, Rinse, Repeat, till 60</p></li> <li><p>Handle Fs as a default case.</p></li> </ul></li> </ul> <p>This will be very long, quite dumb (as in, brute force) and very annoying to write, and a brilliant teaching tool for why you never want to write dumb code again if you can help it.</p> <ul> <li><p>More elegant solution (<em>insert appropriate Star Wars quote here</em>)</p> <ul> <li>What do you see in common between every # that correspons to "A"?</li> </ul> <p>Right, it's the fact that, aside from 100, they all are 2-diit #s starting with 9.</p> <p>So, to handle them all in 1 case statement, wouldn't it be wonderful to switch on some value that gives you "9" for all #s between 90 and 99?</p> <p>I hope you know the answer right away... hint below...</p> <p>Yes, just divide whole by 10!</p> <p>So, you need to have a switch on your numeric grade diveded by 10, with cases of 10, 9, 8, 7, 6 and default mapping to F.</p></li> </ul> <p>Hope this covers enough ground for you to come up with working solution, while showing how the analysis might work as well.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload