Please do not downvote straight away, I'm new here. Just ask for clarification please.
I have an assignment where we are supposed to write a Word Guessing Game.
There are 3 4-lettered, 3 5-lettered and 3 6-lettered questions. (The answers are 4,5 and 6 lettered.)
1 out of each of the 4,5 and 6 letter-answered questions are selected randomly.
The following is where I have the problem:
Below you will see my question and answer arrays (two seperate arrays, as the instructor wanted it this way). Each question corresponds with the answer in the same index. I can call the question randomly, but I don't know how to call the answer accordingly.
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"}
};
The code above are my Q&A arrays.
System.out.println(questions [0] [getRandomNumber(0,3)]);
I am using a random number method to call 1 of 3 questions in the first row, the 4 letter answered questions.
But I do not know how to call the answer accordingly. What I mean is, if the question at [0] [0] is called randomly, I don't know how to call the answer at [0] [0].
Thank you.
CodePudding user response:
Just store the value of the random variable, corresponding the question number. And when getting the answer, USE IT to get the exact answer :)
CodePudding user response:
To get a random number within a certain range, first create the method:
private static int getRandomNumberInRange(int min, int max) {
Random random = new Random();
return random.ints(min, max).limit(1).findFirst().getAsInt();
}
Then simply use the method:
int setIdx = getRandomNumberInRange(0, 3); // you have 3 sets of questions
int questionIdx = getRandomNumberInRange(0, 3); // each set has 3 questions
The max
variable is 3 and not 2 because Random#ints(int, int)
returns a value in range up to max, but not inclusive. Therefore, the maximum value you will get is actually max - 1
which is the last index of your arrays. So, if you want your method to have max
to be inclusive, you will need to modify return random.ints(min, max).
to be return random.ints(min, (max 1))
. It's up to you how you would like to proceed. Whatever you do, document it (Javadoc).
Once you get your indices, you can pass the values to the array. I suggest to create variables for the question and the answer:
String question = questions[setIdx][questionIdx];
String answer = answers[setIdx][questionIdx];
Then, print it out
System.out.println(question " " answer);
One last observation about the getRandomNumberInRange()
method. The call to limit()
is not necessary in this case because the range of numbers you are operating within is very small. If you were dealing with extremely large ranges, limit(n)
returns a stream limited to n
values in the stream. Therefore, limit(1)
returns only one value in the stream. So, if you want, you could just do return random.ints(min, max).findFirst().getAsInt();
. Also up to you. If you ever want to expand this to work on larger ranges, I suggest you leave it as is. There are other optimizations you can do. For example, you don't need to include a min
variable in the getRandomNumbersInRange()
method if you hardcode 0
in Random#ints()
. But, if you ever want to operate with different minimum values in your range, then this becomes an issue. That's why I didn't include that in the code I provided.
UPDATE: Putting it all together
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();
}
Sample 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
UPDATE #2: "Converting the number of letters in answer to array index
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)
This is to select the array of questions and answers based on the (randomly-generated) number of letters in the answers. It is basically a mapping of number of letters to index locations without using Java's Map
class.