I've been set a task to write a muliple choice quiz.
My code calculates the number of correct and incorrect answers to my questions.
However, I've just realised that I'm also supposed to calculate the amount of incorrect inputs (or questions not answered). These are represented by an X in my output. I've coded for these incorrect inputs but can't work out how to add them up & separate them from correct & incorrect answers. My tutor wrote out most of this code & I've modified it to add an array with my own questions. I don't see him until Thursday so I'm a bit lost in the meantime.
Regards,
bresker
import java.util.Scanner;
import java.lang.String;
/**
* MultipleChoiceQuiz question 3 Assignment 2.
*
* @author bresker
* @version 1.0
*/
public class StuartsMultipleChoiceQuiz
{
/**
* This method simply prints the user's input to the console.
*
*/
public static void printResults(String userInput[]){
System.out.println("\nQuestion No.\tUser Input");
for (int i = 0; i < userInput.length; i++){
System.out.println((i + 1) + ".\t\t" + userInput[i]);
} // end for
} // end of method printResults(...)
/**
* This overloaded method prints the correct answers and the
* user's input to the console.
*
*/
public static void printResults(String correctAnswers[], String userInput[]){
System.out.println("Question No.\tCorrect answer\tUser Input");
for (int i = 0; i < correctAnswers.length; i++){
System.out.println((i + 1) + ".\t\t" + correctAnswers[i] + "\t\t" + userInput[i]);
} // end for
} // end of method printResults(...)
/**
* This overloaded method prints the correct answers, the
* user's input and the number of questions answered correctly
* to the console.
*
*/
public static void printResults(String correctAnswers[], String userInput[], int numberCorrectAnswers){
System.out.println("\nQuestion No.\tCorrect answer\tUser Input");
for (int i = 0; i < correctAnswers.length; i++){
System.out.println((i + 1) + ".\t\t" + correctAnswers[i] + "\t\t" + userInput[i]);
} // end for
System.out.println("\nNumber of questions answered correctly: " + numberCorrectAnswers);
} // end of method printResults(...)
/**
* This method calculates the number of correct answers inputted by
* the user and returns it to the calling method.
*
*/
public static int compareResults(String correctAnswers[], String userInput[]){
int numberCorrectAnswers = 0;
for(int i = 0; i < correctAnswers.length; i++){
if (correctAnswers[i].equals(userInput[i])){
numberCorrectAnswers++;
} // end if
} // end for loop
return (numberCorrectAnswers);
} // end compare results
public static void main(String args[]){
final String correctAnswers[] = new String[10];
correctAnswers[0] = "A";
correctAnswers[1] = "D";
correctAnswers[2] = "B";
correctAnswers[3] = "C";
correctAnswers[4] = "D";
correctAnswers[5] = "C";
correctAnswers[6] = "B";
correctAnswers[7] = "A";
correctAnswers[8] = "D";
correctAnswers[9] = "A";
Scanner myScanner = new Scanner(System.in);
String userInput[] = new String[10];
final String questions[] = new String[10];
questions[0] = "What is the capital of Australia? ------- \n \n A - Canberra \n B - Sydney \n C - Perth \n D - Kangaroo Island ";
questions[1] = "What is a Dugong? ------- \n \n A - A bird \n B - A boat \n C - A ghost \n D - A sea creature ";
questions[2] = "How many inches in a foot? ------- \n \n A - 24 \n B - 12 \n C - 36 \n D - A million and one ";
questions[3] = "How many metres in a mile? ------- \n \n A - 2040m \n B - 1212m \n C - 1609m \n D - The House of Commons ";
questions[4] = "The Latin name for which creature is 'pica pica'? ------- \n \n A - The Hippogriff \n B - The blackbird \n C - The Jay \n D - The Magpie ";
questions[5] = "Which Roman emperor made his horse a senatorial consul? ------- \n \n A - Augustus \n B - Constantine \n C - Caligula \n D - Nero ";
questions[6] = "What is the main ingredient of Lava Bread? ------- \n \n A - Nettles \n B - Seaweed \n C - Bran \n D - Moss ";
questions[7] = "What is the largest state in the US of A? ------- \n \n A - Alaska \n B - Colorado \n C - Texas \n D - Hawaii ";
questions[8] = "Where would you find the scapula bone? ------- \n \n A - Thigh \n B - Knee \n C - Chest \n D - Shoulder ";
questions[9] = "What was the UK Christmas Number 1 in 1975? ------- \n \n A - Bohemian Rhapsody \n B - Mary's Boy Child \n C - Merry Xmas Everybody \n D - Lonely this Christmas ";
System.out.println("Welcome to the multiple choice quiz...\n");
System.out.println("Please select a response from A to D..\n");
for (int i = 0; i < 10; i++){
System.out.print(" \nPlease enter answer to question " + (i + 1) + " >> \n\n" + questions[i]);
userInput[i] = myScanner.nextLine();
userInput[i] = userInput[i].toUpperCase();
// Perform some error checking here
if
(userInput[i].equals("A") || userInput[i].equals("B") || userInput[i].equals("C") || userInput[i].equals("D"));
// Do nothing;
else userInput[i] = "X";
} // end for
// printResults(userInput);
// printResults(correctAnswers, userInput);
int numberCorrectAnswers = compareResults(correctAnswers, userInput);
int numberIncorrectAnswers = (10 - numberCorrectAnswers);
printResults(correctAnswers, userInput, numberCorrectAnswers);
System.out.println("\nYou got " + numberIncorrectAnswers + " answers incorrect.");
System.out.println("\nThe pass mark is seven correct answers");
if (numberCorrectAnswers >6)
{ System.out.println("\nWell, done you passed my quiz. You are a STUART'S QUIZ legend.");
}
else
{
System.out.println("\nSorry, you have failed my quiz. You are a STUART'S QUIZ turkey.");
}
} // end main
}

New Topic/Question
Reply




MultiQuote




|