简体   繁体   English

在Java中我如何将数组随机化和整型

[英]In java how can i randomize and int in an array

Hi i am just learning java and want to write a sort of vocabulary trainer. 嗨,我只是在学习Java,并且想写一种词汇训练师。 It is working but i want to give the questions in a random order. 它正在工作,但我想以随机顺序给出问题。 The trainer pics a list from an external file and splits the file in two parts. 培训师会从外部文件中提取一张列表,然后将文件分为两部分。 It is asking as long the next line isn't null. 只要下一行不为空,它就会询问。

for (int i = 0; (zeile = br.readLine()) != null; i++) { 
        splitted = zeile.split(";");                     
        eng[i] = splitted[0];                          
        ger[i] = splitted[1];

then i am asking for the vocabulary. 然后我要词汇。 But as you can see its always in the same order. 但是正如您所看到的,它总是以相同的顺序排列。 I don't know how i can correctly randomize the list before the asking part. 我不知道如何在要求部分之前正确地随机排列列表。

for (int j = 0; j < eng.length; j++) {

        correct = false;
        while (!correct) {

            System.out.print(eng[j] + " bedeutet: ");

            gerEingabe = vokabel.nextLine(); 

            if (gerEingabe.equals(ger[j])) {
                System.out.println("Das ist Korrekt. Auf zur nächsten Aufgabe.");
                correct = true;
            } else {
                System.out.println("Das war leider Falsch. Bitte versuche es noch ein mal.");

It would be nice if someone can help me with this. 如果有人可以帮助我,那就太好了。

To randomize a List , call Collections.shuffle(List<?> list) . 要使List随机化,请调用Collections.shuffle(List<?> list)

To get random integers in a defined range, use Random.nextInt(int n) : 要获得定义范围内的随机整数,请使用Random.nextInt(int n)

Random rnd = new Random();
int val = rnd.nextInt(11) + 5; // Random number 5-15 (inclusive)

Create a small class that holds your translated object. 创建一个小类,其中包含您的翻译对象。 Eg something like below 例如下面的东西

public class Translation {

     public final String originalWord;
     public final String translatedWord;

     public Translation(String original, String translated) {
         this.originalWord = original;
         this.translatedWord = translated;
     }
}

Then add your translations to an ArrayList<Translation> and leverage Collections.shuffle to randomize the ordering. 然后将翻译添加到ArrayList<Translation>并利用Collections.shuffle随机排序。

List<Translation> list = new ArrayList<>();
while((zeile = br.readLine()) != null) {
    splitted = zeile.split(";");                     
    list.add(new Translation(splitted[0], splitted[1]));
}

Then at the beginning, do the shuffle. 然后,开始洗牌。

Collections.shuffle(list);

Iteration can be done by index as well via list.get() , you will just need to adapt the rest of the code to work with a Translation object: 迭代也可以通过list.get()通过索引完成,您只需要修改其余代码即可与Translation对象一起使用:

   for (int j = 0; j < list.size(); j++) {
     Translation t = list.get(j);
     System.out.print(t.originalWord + " bedeutet: " + t.translatedWord);
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM