I am creating a quiz with 15 questions that asks students what the area and perimeter are of a rectangle. I have to keep track of the answers from each question. I am having trouble figuring out how to ask two different questions randomly using JOptionPane
.
Do I need two separate input dialog boxes for each question?
package proj3;
import javax.swing.JOptionPane;
public class Project4App {
public static void main( String args[ ] ) {
Rectangle newRectangle = new Rectangle();
String perimeterQuestion = new String("What is the perimeter of this rectangle?");
String areaQuestion = new String("What is the area of this rectangle?");
int rightAnswers = 0;
int wrongAnswers = 0;
String perimeterAnswer;
int a = 0;
int questionNumber[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
for (int i = 0; i < questionNumber.length; i++ ) {
newRectangle.dimensions();
perimeterAnswer = JOptionPane.showInputDialog("Question #" + questionNumber[0] + "\n" + perimeterQuestion + "\n"
+ newRectangle.toString());
a = Integer.parseInt(perimeterAnswer);
if (a == newRectangle.findPerimeter()) {
rightAnswers += 1;
JOptionPane.showMessageDialog(null, "Congratulations! You got it correct!");
}
else {
wrongAnswers +=1;
JOptionPane.showMessageDialog(null, "The correct answer was " + newRectangle.findPerimeter());
}
questionNumber[0] += 1;
}
}
}