我有一個作業,我們應該寫一個猜詞游戲。
有 3 個 4 字母、3 個 5 字母和 3 個 6 字母問題。(答案是 4,5 和 6 字母。)
從 4,5 和 6 個字母回答的問題中隨機選擇 1 個。
以下是我遇到問題的地方:
您將在下面看到我的問答陣列(兩個單獨的陣列,因為教師希望這樣)。每個問題與同一索引中的答案相對應。我可以隨意呼叫問題,但我不知道如何相應地呼叫答案。
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"}
};
上面的代碼是我的問答陣列。
System.out.println(questions [0] [getRandomNumber(0,3)]);
我正在使用亂數方法呼叫第一行 3 個問題中的 1 個,4 個字母回答問題。
但我不知道如何相應地呼叫答案。我的意思是,如果在[0] [0] 處的問題被隨機呼叫,我不知道如何在[0] [0] 處呼叫答案。
uj5u.com熱心網友回復:
只需存盤隨機變數的值,對應問題編號。當得到答案時,用它來得到確切的答案:)
uj5u.com熱心網友回復:
要獲取一定范圍內的亂數,首先創建方法:
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是陣列的最后一個索引。因此,如果您希望您的方法必須具有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)回傳一個僅限于流中n值的流。因此,limit(1)只回傳流中的一個值。所以,如果你愿意,你可以做return random.ints(min, max).findFirst().getAsInt();。也取決于你。如果您想擴展它以在更大的范圍內作業,我建議您保持原樣。您還可以進行其他優化。例如,您不需要包括min在變數getRandomNumbersInRange()的方法,如果你硬編碼0在Random#ints(). 但是,如果您想在您的范圍內使用不同的最小值進行操作,那么這就會成為一個問題。這就是為什么我沒有在我提供的代碼中包含它。
更新:把它們放在一起
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();
}
樣本輸出
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)
這是根據答案中(隨機生成的)字母的數量選擇問題和答案的陣列。它基本上是將字母數映射到索引位置而不使用 Java 的Map類。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/396858.html
