6

我正在尝试阅读 Java 中的文本文件,基本上是一组问题。有四个选择和一个答案。结构如下所示:

问题

选项一

选项 b

选项c

选项d

回答

我可以毫无困难地这样阅读:

public class rar{
public static String[] q=new String[50];
public static String[] a=new String[50];
public static String[] b=new String[50];
public static String[] c=new String[50];
public static String[] d=new String[50];
public static char[] ans=new char[50];
public static Scanner sr= new Scanner(System.in);


public static void main(String args[]){
int score=0;
try {
             FileReader fr;
      fr = new FileReader (new File("F:\\questions.txt"));
      BufferedReader br = new BufferedReader (fr);
int ar=0;
      for(ar=0;ar<2;ar++){
      q[ar]=br.readLine();
      a[ar]=br.readLine();
      b[ar]=br.readLine();
      c[ar]=br.readLine();
      d[ar]=br.readLine();
    String tempo=br.readLine();
    ans[ar]=tempo.charAt(0);
    
      
      
      
    
      
        System.out.println(q[ar]);
        System.out.println(a[ar]);
        System.out.println(b[ar]);
        System.out.println(c[ar]);
        System.out.println(d[ar]);
        System.out.println("Answer: ");
        String strans=sr.nextLine();
char y=strans.charAt(0);
if(y==ans[ar]){
    System.out.println("check!");
score++;
System.out.println("Score:" + score);
}else{
System.out.println("Wrong!");
}
        
      }
      br.close();
    } catch (Exception e) { e.printStackTrace();}


}

 


}

上面的代码是可以预测的。for 循环只是递增。它会按顺序显示问题。

我想要做的是能够通过文本文件随机化,但仍然保持相同的结构。(q, a, b, c, d, ans)。但是当我尝试这样做时:

int ran= random(1,25);
   System.out.println(q[ran]);
        System.out.println(a[ran]);
        System.out.println(b[ran]);
        System.out.println(c[ran]);
        System.out.println(d[ran]);
        System.out.println("Answer: ");
        String strans=sr.nextLine();
char y=strans.charAt(0);
if(y==ans[ran]){
    System.out.println("check!");
score++;
System.out.println("Score:" + score);
}else{
System.out.println("Wrong!");
}

这是我用于随机化的方法:

public static int random(int min, int max){
    int xx;
    xx= (int) ( Math.random() * (max-min + 1))+ min;
    return xx;
    }

我有可能得到一个空值。您可以建议我做些什么,以便在尝试随机化问题时不会得到空值?

你能看出我的程序还有什么问题吗?

4

4 回答 4

4

我认为一些结构上的改变会有很大帮助,让你更容易做到这一点。定义新类:QuestionAnswer. 让我们Question拥有选项和Answer其中的内容。那是对象组合。

查看Collection API。使用一系列问题,您可以使用 shuffle 方法将它们随机排列在一行中。让 Java 为您完成工作。

所以你可能有:

Collection<Question> questions = new ArrayList<Question>();

questions.add(...);
questions.add(...);
questions.add(...);

questions.shuffle();

为了更详细地说明您为什么要这样做,为什么……您想尽可能地分离您的关注点。问题、答案和选项都是不同的关注点。用户的反应令人担忧。问题的随机化是一个问题。对用户响应的响应是一个问题。

作为一名优秀的软件开发人员,您将希望将所有这些东西分开。Java 实现这一点的构造是类。你可以在他们自己的班级内相对独立地发展你的想法。当您对您的课程感到满意时,您所要做的就是将它们连接起来。定义他们的接口,他们如何相互交谈。我喜欢先定义接口,但是当我开始时,我发现以后担心这个会更容易一些。

可能看起来有很多工作,所有这些类和接口等等。当你变得很好时,这样做只需要一小部分时间。你的奖励是可测试性的可重用性。

于 2010-10-08T11:54:50.277 回答
2

其他人(Mike、Erick)已经提出了解决这个问题的更好方法,方法是创建一个新Question类,将问题添加到集合中,并使用该shuffle方法将它们随机化。

关于为什么你在你的代码中“得到一个空值”:据我在你的示例代码中看到的,你只从文件中读取两个问题:

for (ar=0;ar<2;ar++) {
    [...]
}

这意味着数组中的位置 0 和 1 将包含有效数据,而位置 2 到 49 将包含null.

稍后,当您尝试随机化问题时,您会random这样调用您的方法:

int ran = random(1,25);

这将返回 1 到 25 之间的值,然后将其用作数组索引。

如果这个索引恰好是“1”,你会没事的。对于所有其他情况(2 到 25),您将访问null数组中的值,并在尝试使用这些值时遇到异常。

于 2010-10-08T12:44:28.600 回答
2

你在你的代码中使用了各种神奇的数字,这些数字没有多大意义。

public static String[] q=new String[50]; //why make an array to hold 50 questions?

//... 

for(ar=0;ar<2;ar++){ //why read 2 questions?

//...

int ran= random(1,25); //why take one of 25 questions?
System.out.println(q[ran]);

这些都应该是相同的数字,对吧?如果我们有 25 个问题,我们应该有 25 个问题,阅读 25 并使用 25。

如何解决这个问题:

1 做一个常数

public final static int NUMBER_OF_QUESTIONS = 25;

然后在制作数组、阅读问题和随机抽取问题时使用它:

public static String[] q=new String[NUMBER_OF_QUESTIONS];

for(ar=0;ar<NUMBER_OF_QUESTIONS;ar++){

int ran= random(1,NUMBER_OF_QUESTIONS);

2 使用 q.length

public static String[] q=new String[NUMBER_OF_QUESTIONS];

for(ar=0;ar<q.length;ar++){

int ran= random(1,q.length);

3 使用列表/集合

public static List<String> q=new List<String>();

for(ar=0;ar<q.size();ar++){

int ran= random(1,q.size());

选项 3 将是最佳选择,毕竟这是 java。请参阅 Mike 的答案以获取更多关于使这个更 Java 的详细信息。

于 2010-10-08T14:12:02.430 回答
1

创建一个类来保存一个问题并将文件读入这些对象的数组中。

把问题分成三个步骤。第一步是读取数据文件并将所有数据存储在对象中。第二步是随机化这些对象的顺序。最后一步是将它们打印出来。

ArrayList 问题 = new ArrayList();
for(ar=0;ar<2;ar++){
  q=br.readLine();
  a=br.readLine();
  b=br.readLine();
  c=br.readLine();
  d=br.readLine();
  字符串节奏=br.readLine();
  ans=tempo.charAt(0);

  questions.add(新问题(q, a, b, c, d, ans));
}

像这样随机化数组:

Collections.shuffle(问题);

然后只需循环问题并输出它们。

对于(问题 q:问题){
  q.write();
  System.out.println(); // 问题之间的空格
}

创建一个这样的问题类来保存您的数据:

公共课问题{
  私人字符串问题;
  私有字符串选项1;
  私有字符串选项2;
  私有字符串选项3;
  私有字符串选项4;
  私人字符串答案;

  公共问题(字符串问题,字符串选项1,字符串选项2,字符串选项3,
                  字符串选项4,字符串答案){
    this.question = 问题;
    this.option1 = 选项1;
    this.option2 = 选项2;
    this.option3 = 选项3;
    this.option4 = 选项4;
    this.answer = 答案;
  }

  公共无效写入(){
    System.out.println(this.question);
    System.out.println(this.option1);
    System.out.println(this.option2);
    System.out.println(this.option3);
    System.out.println(this.option4);
    System.out.println("答案:"+this.answer);
  }
}
于 2010-10-08T11:58:49.607 回答