随机化用Java读取的文本文件 [英] Randomizing text file read in Java

查看:84
本文介绍了随机化用Java读取的文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Java读取一个文本文件,基本上是一组问题。有四个选择和一个答案。结构如下所示:

I am trying to read a text file in Java, basically a set of questions. With four choices and one answer. The structure looks like this:


问题

question

选项a

选项b

选项c

选项d

回答

我可以通过这种方式阅读:

I have no trouble reading it that way:

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循环只是递增。它会根据订单显示问题。

The code above is predictable. The for loop just increments. And it displays the questions based on order.

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

What I want to do is to be able to randomize through the text file, but still maintaining the same structure. (q, a, b, c, d, ans). But when I try to do this:

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!");
}

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

And this is the method that I use for randomizing:

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

我有可能得到一个空值。当你试图随机化问题时,你能建议我这样做我没有得到空吗?

There's the possibility that I get a null. What can you recommend that I would do so that I get no null when trying to randomize the questions?

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

Can you see anything else that is wrong with my program?

推荐答案

你使用各种幻数,代码中的数字没有多大意义。

You use all kind of magic numbers, numbers in your code that don't make much sense.

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个。

These should all be the same number, right? If we have 25 questions, we should have room for 25, read 25 and use 25.

public final static int NUMBER_OF_QUESTIONS = 25;

然后在制作数组时使用它,阅读问题并随机获取:

Then use that when making the array, reading the questions and when taking a random one:

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



2 Use 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使用清单/收藏



3 Use a List / Collection

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

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

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

选项3将是最佳选择,这是java afterall。有关更多详细信息,请参阅Mike的答案。

Option 3 would be the best choice, this is java afterall. See Mike's answer for more detail in making this more Java.

这篇关于随机化用Java读取的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆