I want to ask the user about different states and output a score.
The program does everything correctly
Instead at the end it outputs exception in thread "main" instead of outputting the final score
import java.util.*;
class StateQuiz{
public static void main( String[] args ){
boolean[] asked = new boolean[10];
boolean[] correct = new boolean[10];
String [] answers={"Alaska","Ohio","Illinois","Ohio","Florida","Hawaii","New York","California","Maryland","Texas"};
String [] questions={"What is the largest state?","Where is the city of Columbus located?","Where is the city of Springfield located?","Where is Ohio State located?","What is the orange state?","What is the most southern state?","Where is the Big Apple?","Where is Hollywood?","What state is Baltimore located?","What state looks like a boot?"};
int nextQ =-1;
do{
nextQ = getNextQuestion(asked);
boolean ok = quizUser(answers,questions,nextQ);
asked[nextQ] = true;
if (ok == true) {
correct[nextQ] = true;
} else {
correct[nextQ] = false;
}
}while(nextQ!=-1);
printResults(correct);
}
public static int getNextQuestion(boolean[] questionsAsked){
int cnt=0;
int[] x = {};
for (int i = 0; i < questionsAsked.length; i++) {
if (questionsAsked[i] == false) {
cnt++;
}
}
if (cnt == 0)
return -1;
int ix=0;
do{
ix = (int)(Math.random() * questionsAsked.length);
}while(questionsAsked[ix]==true);
return ix;
}
public static boolean quizUser(String[]answers,String[]questions, int nextQ) {
do{
System.out.println(questions[nextQ]);
String an;
Scanner keyboard = new Scanner( System.in );
an = keyboard.nextLine();
if(an.equalsIgnoreCase(answers[nextQ])){
System.out.println("Correct");
return true;
}else{
System.out.println("Incorrect");
return false;
}
}while(nextQ!=0);
}
public static void printResults (boolean[] questions){
int correctQuestions = 0;
for(int p=0; p < questions.length; p++) {
if (questions[p]) {
correctQuestions++;
}
}
double average = correctQuestions / questions.length;
System.out.println("Your average score is: " + average + "%");
if (average >= 80) {
System.out.println ("Wow! You really know a lot about states!");
} else if (average >= 40 && average < 80 ) {
System.out.println ("Apparently you know some states.");
} else if (average < 40) {
System.out.println ("You should study up on states.");
}
}
}
*I want to keep the program simple and I don't want to change the program too much
Aucun commentaire:
Enregistrer un commentaire