谁能帮我用二维数组重写这段代码来循环并显示选择?
以下是问答式游戏。我有一个包含程序读取的数据的文本文件,所以首先是问题,然后是 4 个答案选项,然后是答案(文件中有 20 个问题):
1) What is 1+2 = ...?
a) 2
b) 3
c) 0
d) 1
b
...
这是代码:
import java.io.File;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class GameQuiz
{
public static void main(String[] args) {
String[] data = new String[120];
String question = "";
int count = 0;
try{
Scanner inFile = new Scanner(new File("questions.txt"));
while (inFile.hasNext()){
data[count] = inFile.nextLine();
count++;
}
inFile.close();
}catch(Exception e){
e.printStackTrace();
}
int choice = 0, numCorrect = 0, i = 0;
char correctAnswer = ' ', correct = ' ';
String answer= "";
String answerChoices[] = {"a", "b", "c", "d"};
for (int j = 0; j < 20; j++)
{
answer = "";
for (i = 0; i < 5; i++)
{
answer += data[i+(j*6)] + "\n";
}
correctAnswer = data[i+(j*6)].charAt(0);
choice = JOptionPane.showOptionDialog(null, answer, "Quiz Game",
0, 3, null, answerChoices, null);
if (choice == 0) correct = 'a';
if (choice == 1) correct = 'b';
if (choice == 2) correct = 'c';
if (choice == 3) correct = 'd';
if (correct == correctAnswer)
{
numCorrect ++;
JOptionPane.showMessageDialog(null, "Correct!");
}
else JOptionPane.showMessageDialog(null, "Sorry.\n"+
"The correct answer was "+correctAnswer);
}
JOptionPane.showMessageDialog(null, "You have "+numCorrect+"/20 correct answers.");
}
}