Quantcast
Channel: User raptortech97 - Code Review Stack Exchange
Viewing all articles
Browse latest Browse all 22

Answer by raptortech97 for Issuing multiple choice tests

$
0
0

You never use uSelect1. Get rid of it. You don't care which incorrect answer the user chooses, so let's consolidate. If you went with a select statement, it would look like this:

switch(uSelect1.toUpperCase()) {    case "B":        score++;        System.out.println("Correct!");        break;    case "A":    case "C":    case "D":        System.out.println("Incorrect.");        break;    default:        System.out.println("Unrecognized input");}

Note that default is the default case, when the string is not otherwise matched. The chained A-C-D thing is something called switch statement fall through, but you don't need to worry about that. Also more that println is very nice because it adds that "\n" at the end for us.

A switch statement, however, is not the way to do this. The best way to do this would be to have a string variable correctSelection set to "B" so we can say

char selection = uSelect1.toUpperCase().charAt(0);if (selection == 'B') {    score++;    System.out.println("Correct!");} else if ('A'<= selection && selection <= 'D') {    System.out.println("Incorrect");} else {    System.out.println("Bad Input");}

Once you feel more comfortable with loops and arrays, this can be done a little more easily.

Also, your variable names are a little odd. What's with this u and 1 stuff? The user selected a letter, so call it a selectedLetter (I only used selection in the above code because I'm typing this on mobile.)

Good luck!


Viewing all articles
Browse latest Browse all 22

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>