簡體   English   中英

如何存儲從字符串數組中隨機選擇的問題?

[英]How can I store a randomly selected question from a String array?

請不要立即投票,我是新來的。 請要求澄清。

我有一個任務,我們應該寫一個猜詞游戲。

有 3 個 4 個字母的問題、3 個 5 個字母的問題和 3 個 6 個字母的問題。 (答案是 4,5 和 6 個字母。)

4,5 和 6 個字母回答問題中的每一個都是隨機選擇的。

以下是我遇到問題的地方:

下面你會看到我的問題和答案 arrays(兩個單獨的 arrays,因為教練想要這樣)。 每個問題對應於同一索引中的答案。 我可以隨機調用問題,但我不知道如何相應地調用答案。

public static String questions [] [] = { {"What is the opposite of bad?", "What is a baby sheep called?", "What is an informal test or examination of a student or class?"},
                                         {"A popular Italian dish", "Which is the country who suffered from 2 nuclear attacks in WW2?", "An ancient manuscript text in book form"},
                                         {"An activity where the player aims to connect pieces to create an image", "A person who rides a horse in a race", "Something that is one of a kind"}                          
                                       };

public static String answers [] [] = { {"Good", "Lamb", "Quiz"},
                                       {"Pizza", "Japan", "Codex"},
                                       {"Puzzle", "Jockey", "Unique"}
                                     };

上面的代碼是我的問答 arrays。

System.out.println(questions [0] [getRandomNumber(0,3)]);

我正在使用隨機數方法調用第一行 3 個問題中的 1 個,4 個字母回答了問題。

但我不知道如何相應地調用答案。 我的意思是,如果[0] [0]處的問題被隨機調用,我不知道如何調用[0] [0]處的答案。

謝謝你。

只需存儲隨機變量的值,對應問題編號。 當得到答案時,用它來得到確切的答案:)

要獲取一定范圍內的隨機數,首先創建方法:

private static int getRandomNumberInRange(int min, int max) {

    Random random = new Random();
    return random.ints(min, max).limit(1).findFirst().getAsInt();
}

然后簡單地使用方法:

int setIdx = getRandomNumberInRange(0, 3); // you have 3 sets of questions
int questionIdx = getRandomNumberInRange(0, 3); // each set has 3 questions

max變量是 3 而不是 2,因為Random#ints(int, int)返回一個最大范圍內的值,但不包括在內。 因此,您將獲得的最大值實際上是max - 1 ,這是 arrays 的最后一個索引。 因此,如果您希望您的方法具有包含max的最大值,則需要修改return random.ints(min, max). return random.ints(min, (max + 1)) 由您決定如何進行。 無論您做什么,都要記錄下來(Javadoc)。

獲得索引后,您可以將值傳遞給數組。 我建議為問題和答案創建變量:

String question = questions[setIdx][questionIdx];
String answer = answers[setIdx][questionIdx];

然后,打印出來

System.out.println(question + " " + answer);

關於getRandomNumberInRange()方法的最后一個觀察結果。 在這種情況下,不需要調用limit() ,因為您操作的數字范圍非常小。 如果您正在處理非常大的范圍, limit(n)將返回 stream,限制為 stream 中的n值。 因此, limit(1)只返回 stream 中的一個值。 所以,如果你願意,你可以做return random.ints(min, max).findFirst().getAsInt(); . 也取決於你。 如果您想擴展它以在更大的范圍內工作,我建議您保持原樣。 您還可以進行其他優化。 例如,如果在Random#ints()中硬編碼0 ,則不需要在getRandomNumbersInRange()方法中包含min變量。 但是,如果您想在您的范圍內使用不同的最小值進行操作,那么這將成為一個問題。 這就是為什么我沒有在我提供的代碼中包含它。

更新:把它們放在一起

public static void main(String[] args) {

    final String questions[][] = {
            { "What is the opposite of bad?", "What is a baby sheep called?",
                    "What is an informal test or examination of a student or class?" },
            { "A popular Italian dish:", "Which is the country who suffered from 2 nuclear attacks in WW2?",
                    "An ancient manuscript text in book form:" },
            { "An activity where the player aims to connect pieces to create an image:",
                    "A person who rides a horse in a race:", "Something that is one of a kind:" } };

    final String answers[][] = { { "Good", "Lamb", "Quiz" }, { "Pizza", "Japan", "Codex" },
            { "Puzzle", "Jockey", "Unique" } };

    for (int i = 0; i < 10; i++) {
        int setIdx = getRandomNumberInRange(0, 2); // you have 3 sets of questions
        int questionIdx = getRandomNumberInRange(0, 2); // each set has 3 questions

        String question = questions[setIdx][questionIdx];
        String answer = answers[setIdx][questionIdx];
        System.out.println(question + " " + answer);
    }
}

/**
 * @param min minimum value in range (inclusive)
 * @param max maximum value in range (inclusive)
 * @return a pseudorandom number within the given range. For instance
 *         <code>getRandomNumberInRange(4, 6)</code> will return a 4, 5, or a 6.
 */
private static int getRandomNumberInRange(int min, int max) {

    Random random = new Random();
    return random.ints(min, max + 1).limit(1).findFirst().getAsInt();
}

樣品 output

A person who rides a horse in a race: Jockey
An ancient manuscript text in book form: Codex
What is an informal test or examination of a student or class? Quiz
What is a baby sheep called? Lamb
A popular Italian dish: Pizza
What is a baby sheep called? Lamb
What is a baby sheep called? Lamb
What is a baby sheep called? Lamb
Which is the country who suffered from 2 nuclear attacks in WW2? Japan
A person who rides a horse in a race: Jockey

更新#2 :“將字母數轉換為數組索引

int randomNumber = getRandomNumberInRange(4, 6); // to generate a number based on number of letters in answers
int answerArrayIdx = randomNumber % 4; // converts 4, 5, or 6 into 0, 1,  or 2 (the corresponding array index location)

這是 select 基於答案中(隨機生成的)字母數量的問題和答案數組。 它基本上是字母數量到索引位置的映射,而不使用 Java 的Map class。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM