patternjavaMinor
Issuing multiple choice tests
Viewed 0 times
issuingchoicetestsmultiple
Problem
I'm working on code for an assignment and I was hoping you all could point me in the right direction.
Basically I had to write a program that will issue out a multiple choice test. I have 4 options, A, B, C, D and if the user accidentally chooses something other than my options I want it to ask the question over again.
I've messed around with loops and counters but I'm really hitting a standstill right here. I seem to be unable to get this to work so I'd like you to review my code as is. The code at it's current state here is working, although it does not do much.
I'm up for whatever critique, so if you see something a beginning Java student should do, please let me know.
```
public static void main(String[] args) {
String[] multiChoice1;
Scanner input = new Scanner(System.in);
multiChoice1 = new String[5];
multiChoice1[0] = "1. Which country currently emits the most greenhouse gases?\n";
multiChoice1[1] = "blah blah";
multiChoice1[2] = "blah blah";
multiChoice1[3] = "blah blah";
multiChoice1[4] = "blah blah";
String userSelect1 = multiChoice1[0];
if(userSelect1==multiChoice1[0]){
System.out.println(multiChoice1[0]);
System.out.println("A. United States");
System.out.println("B. China");
System.out.println("C. India");
System.out.println("D. England");
System.out.println();
System.out.print("Your answer: ");
String uSelect1 = input.next();
switch (uSelect1.toUpperCase()){
case "A":
uSelect1 = "United States";
System.out.println("Incorrect!\n\n");
break;
case "B":
uSelect1 = "China";
System.out.println("Correct!\n\n");
scorePro++;
score++;
break;
case "C":
uSelect1 = "India";
System.out.println("Incorrect!\n\n");
break;
case "D"
Basically I had to write a program that will issue out a multiple choice test. I have 4 options, A, B, C, D and if the user accidentally chooses something other than my options I want it to ask the question over again.
I've messed around with loops and counters but I'm really hitting a standstill right here. I seem to be unable to get this to work so I'd like you to review my code as is. The code at it's current state here is working, although it does not do much.
I'm up for whatever critique, so if you see something a beginning Java student should do, please let me know.
```
public static void main(String[] args) {
String[] multiChoice1;
Scanner input = new Scanner(System.in);
multiChoice1 = new String[5];
multiChoice1[0] = "1. Which country currently emits the most greenhouse gases?\n";
multiChoice1[1] = "blah blah";
multiChoice1[2] = "blah blah";
multiChoice1[3] = "blah blah";
multiChoice1[4] = "blah blah";
String userSelect1 = multiChoice1[0];
if(userSelect1==multiChoice1[0]){
System.out.println(multiChoice1[0]);
System.out.println("A. United States");
System.out.println("B. China");
System.out.println("C. India");
System.out.println("D. England");
System.out.println();
System.out.print("Your answer: ");
String uSelect1 = input.next();
switch (uSelect1.toUpperCase()){
case "A":
uSelect1 = "United States";
System.out.println("Incorrect!\n\n");
break;
case "B":
uSelect1 = "China";
System.out.println("Correct!\n\n");
scorePro++;
score++;
break;
case "C":
uSelect1 = "India";
System.out.println("Incorrect!\n\n");
break;
case "D"
Solution
As you might have noted, methods which do printing are of no use in a bigger piece of code. While your lengthy method tries to say something to the user, it fails to communicate the outcome to the caller.
You could add some return value to the printing... but that's like adding insult to injury. Try to write methods computing something, call them, and react by some printing.
Try to start at the top level like
Now, there's a s*load of methods to write... but all of them are pretty trivial. You may also find out that they need more arguments or whatever... there are many ways to go. Just don't print in methods which may need to be reused. Every method should do a single thing.
You could add some return value to the printing... but that's like adding insult to injury. Try to write methods computing something, call them, and react by some printing.
Try to start at the top level like
while (true) {
printQuestion();
char c = readInput();
if (!isLegalInput(c)) continue; // i.e., jump to the loop start to ask again
if (isCorrectAnswer(c)) {
print("Correct!\n\n");
} else {
print("Inorrect!\n\n");
}
}Now, there's a s*load of methods to write... but all of them are pretty trivial. You may also find out that they need more arguments or whatever... there are many ways to go. Just don't print in methods which may need to be reused. Every method should do a single thing.
Code Snippets
while (true) {
printQuestion();
char c = readInput();
if (!isLegalInput(c)) continue; // i.e., jump to the loop start to ask again
if (isCorrectAnswer(c)) {
print("Correct!\n\n");
} else {
print("Inorrect!\n\n");
}
}Context
StackExchange Code Review Q#64472, answer score: 7
Revisions (0)
No revisions yet.