我正在创建一个包含15个问题的测验,询问学生区域和周长是多少。我必须跟踪每个问题的答案。我无法弄清楚如何使用JOptionPane
随机提出两个不同的问题。
每个问题是否需要两个单独的输入对话框?
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;
}
}
}
答案 0 :(得分:2)
ArrayList
Collections.sort(...)
按随机顺序对问题进行排序重复步骤4-5直到完成。
答案 1 :(得分:1)
最简单的解决方案是将所有答案都放入数组中。然后获取数组大小范围内的随机数(How do I generate random integers within a specific range in Java?),然后从所有问题的数组中选择该索引。然后将对话框的值设置为该值。这将创建您的问题的随机化。