Java 中的二维数组和数据文件
2D Array in Java with Data Files
谁能帮我重写这段代码,使用二维数组循环显示选项?
以下为问答类游戏。
我有一个包含程序读取的数据的文本文件,
所以首先是问题,然后是 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.");
}
}
你不需要改变太多:
while (inFile.hasNext()){
for(int i = 0; i <20; i++){
for(int j = 0;j <6; j++){
data[i][j] = inFile.nextLine();
count++;
}
}
}
和:
for (i = 0; i < 5; i++){
answer += data[j][i] + "\n";
}
correctAnswer = data[j][i].charAt(0);
它应该可以正常工作。
但是,您应该按照评论中的建议考虑更好的解决方案。这不是最有效的方法。
编辑:
您还需要更改 data[] 声明:
String[][] data = new String[20][6];
谁能帮我重写这段代码,使用二维数组循环显示选项?
以下为问答类游戏。 我有一个包含程序读取的数据的文本文件, 所以首先是问题,然后是 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.");
}
}
你不需要改变太多:
while (inFile.hasNext()){
for(int i = 0; i <20; i++){
for(int j = 0;j <6; j++){
data[i][j] = inFile.nextLine();
count++;
}
}
}
和:
for (i = 0; i < 5; i++){
answer += data[j][i] + "\n";
}
correctAnswer = data[j][i].charAt(0);
它应该可以正常工作。 但是,您应该按照评论中的建议考虑更好的解决方案。这不是最有效的方法。
编辑: 您还需要更改 data[] 声明:
String[][] data = new String[20][6];