Quiz Code Java
Quiz Code Java
import javax.swing.JOptionPane;
do
{
answer = JOptionPane.showInputDialog(null, fmtQuestion);
if(answer == null)
{
int quit = JOptionPane.showConfirmDialog(null, "Would you
like to quit?", "Quit", JOptionPane.YES_NO_OPTION);
if(quit == 0)
return "ABORT";
else
continue;
}
} while (answer == null || !(isValid(answer)));
return answer;
}
/**
* isCorrect recieves two values a (correct answer) and r (incorrect
* answer). Returning a boolean value if the answer is correct or not
*/
private static boolean isCorrect(String a, String r) // a = correct answer, r
= user response
{
r = r.toUpperCase();
if(a.equals(r))
{
JOptionPane.showMessageDialog(null, "Correct!");
return true;
}
else
{
JOptionPane.showMessageDialog(null, "The correct answer is: \n" +
a);
return false;
}
}
/**
* showGrade accepts two values c (total correct answers) and i (total
* incorrect answers). A message dialog is displayed stating the total
* coreect, incorrect, and the grade for the test
*/
public static void showGrade(int c, int i)
{
int numberQuestions = c + i;
String fmtGrade = "";
int pointsPerQuestion = 100 / numberQuestions;
int grade = c * pointsPerQuestion;
JOptionPane.showMessageDialog(null, fmtGrade);
}
/*
* I know multi-dimensional arrays are not till next chapter but
* I already knew about them and felt that it really was the best
* option for this assignment
*/
i++;
} while(i < question.length);
showGrade(correct, incorrect);
}
}
Contact GitHub API Training Shop Blog About
� 2017 GitHub, Inc. Terms Privacy Security Status Help