for(int j = 0; j < questions.length; j++) {
int randomIndex = B[j];
String input = JOptionPane.showInputDialog(null, questions[randomIndex]);
if(answers[randomIndex].equalsIgnoreCase(input))
{
count++; // incrementing counter if entered answer is correct
point++;
correct++;
JOptionPane.showMessageDialog(null, "Congratulations that is correct!");
}
else
{
JOptionPane.showMessageDialog(null, "Wrong Answer!");
}
if(!timeForMore) // if time is over, the program executes the loop an stops asking questions.
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 " + point + " % ");
try {
BufferedWriter out;
String name = JOptionPane.showInputDialog(null, "Enter your name");
String type = JOptionPane.showInputDialog(null, "Enter your quiz type");
if(type.equals("Plate Tectonics")){
out = new BufferedWriter(new FileWriter("players.txt",true));
out.write(name); //Writing to the textfile (the name entered by user)
out.write(getSpace(20- name.length())); // Here 20 is max length possible change accordingly
String trimmedStr = name.trim();
out.write(String.valueOf(points+ " % "));
out.write(" ");
out.write(type);
out.newLine(); //write a new line to the textfile so the next time it writes to the file it does it on the next line
out.close();
for(int i=1; i<points.length; i++) {
for(int j=0; j<points.length-i; j++) { //if in wrong order then swap
if (points[j]>points[j+1]) swap(points, j, j+1);
}
}
}
private static void swap (int[] points, int i, int j) {
int h = points[i];
points[i] = points[j];
points[j] = h;
}