the for loop isn't working
Let's have a look at the
askQuestions method.
public void askQuestions(String[] questions, String[] answers) {
// To begin with, are you certain that the questions array
// and the answers array are the same length and have non-null
// elements? If so, okay. If not, this is a good place to check.
// See comment below this method for more about this.
int count = 0;
int point = 0;
for(int j = 0; j < questions.length; j++) {
// The "timeForMore" is a member variable (declared in
// class scope) and is used as a flag, set in the
// stopAndShowResults method by another class.
// If you set it to true for each trip thru this
// loop you may interfere with its function which is to
// wait until the timing class notifies this class that
// the time is up and we should break out of the
// 'askingQuestions' loop. This should be reset when you
// start timing each question_and_answer session; perhaps
// when you enter this method but possibley not. So this
// next line should be removed from here.
timeForMore = true;
// You will get better results with a Random instance if
// you declare it as a member variable (in class scope)
// rather than making a new one for each trip thru this
// loop. So move this next line to the member variable
// declarations area up above the constructor.
Random generator = new Random();
// Question for later - how will you prevent "generator"
// from returning duplicate indices?
int randomIndex = generator.nextInt(questions.length);
String input = JOptionPane.showInputDialog(null, questions[randomIndex]);
the counter isn't being incremented
// There are no curley braces after this if
// statement to form a code block so the next statement,
// viz, "count++;", is the only statement affected/protected
// by this if statement. If you add a curley brace
// after the if statement and after the "point++"
// statement then boith statements will be
// included_in/controlled_protected_by the if statement.
if(answers[randomIndex].equalsIgnoreCase(input))
count++; // incrementing counter if entered answer is correct
point++;
// if time is over, the program executes the loop an stops
// asking questions.
if(!timeForMore)
break;
}
JOptionPane.showMessageDialog(null, "You answered " + count +
" out of " + questions.length +
" questions correctly.");
int percent = (count*100)/questions.length;
JOptionPane.showMessageDialog(null, "Your Geography Quiz score is " +
percent + " % ");
}
I asked about the "questions" array argument to the
askQuestions method because of this:
try {
while (true) {
// Here you are saving the read input in the "desc" variable but
// you do not add it ("desc") to the StringBuilder. So when the
// StringBuilder is returned as a string it will be empty.
desc = in.readUTF();
}
} catch (EOFException e) { }
when time is over it still continues displaying the questions
Could be many things. First remove the reassignment of "timeForMore" from the
for loop, mentioned above. Then, as a place to start, since we are working in/on the
askQuestions method, try printing out the value of "timeForMore" in the "askQuestions"
for loop to see if the timing class changes this flags value when the time has expired.
It doesn't even check whether the answers are correct or not.
This could be because of a zero–length array being passed to the
askQuestions method as the "questions" argument.
Note: I see you have made an edit since I started writing this.