我在我的程序结束时,我试图在arraylist中创建一个随机数。我有下面的语法我正在使用。我不知道如何写这个,但我希望代码显示我想要完成的任务。
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class TriviaQuestionTester {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
File gameFile = new File("trivia.txt");
String nextLine, category;
int cat1 = 0;
int cat2 = 0;
int cat3 = 0;
int cat4 = 0;
int cat5 = 0;
int cat6 = 0;
int random1, random2, random3, random4, random5, random6;
int totalScore = 0;
int awardedPoints = 200;
String answer;
Random random = new Random();
Scanner inFile = new Scanner(gameFile);
Scanner scanner = new Scanner(System.in);
ArrayList <String> myList = new ArrayList<String>();
//append @ sign to first index of arraylist when we copy the text file into it. first line will
//be the first category.
category = "@"+ inFile.nextLine();
//add category to arraylist
myList.add(category);
//while textfile has another line of input, write that line into the arraylist.
while (inFile.hasNextLine()){
nextLine = inFile.nextLine();
//check if the next line is blank. if it is, advance to the next line (as it must be a category) and append @ sign
//before copying it into the arraylist. if it is not, write the line into the arraylist.
if(nextLine.equals("")){
category = "@"+ inFile.nextLine();
myList.add(category);
} else {
myList.add(nextLine);
}
}
//close the text file from reading
inFile.close();
System.out.println("Steve's Crazy Trivia Game!!!!" );
System.out.println("");
//find categories by searching the contents of every arraylist index and looking for the @ sign.
//when you find that, assign that into the respective variable.
int q = 1;
for(int j = 0; j < myList.size(); j++){
if(myList.get(j).contains("@")){
//System.out.println("Category found at " + j);
if(q == 1){
cat1 = j;
}
if(q == 2){
cat2 = j;
}
if(q == 3){
cat3 = j;
}
if(q == 4){
cat4 = j;
}
if(q == 5){
cat5 = j;
}
if(q == 6){
cat6 = j;
}
q++;
}
}
//first question
//get category from variable, print it to user without the @ sign
System.out.println("Category: " + myList.get(cat1).substring(1));
//generate a random number in a range 1 more than the index where the category is and 2 less than
//where the next category is to get a question (always will be odd numbers)
//Credit: Derek Teay
random1 = random.nextInt(((cat2 - 2) - (cat1 + 1))) + (cat1 + 1);
//debug code - shows the number generated
//System.out.println("Random number: " + random1);
//take the modulus of the random number. if there is a remainder, the number is odd, continue.
//if there is not a remainder, number is even, generate a new number until the randomly generated
//number is odd.
while (random1 % 2 == 0){
random1 = random.nextInt(((cat2 - 2) - (cat1 + 1))) + (cat1 + 1);
//debug code - shows the new random number if the first number isn't odd
//System.out.println("New random number: " + random1);
}
//display the question to the user
System.out.println("Question: " + myList.get(random1));
//accept user input
System.out.print("Please type your answer: ");
//store the user answer in a variable but lowercase
answer = scanner.nextLine().toLowerCase();
System.out.println();
//display the officially correct answer from the arraylist
//System.out.println("Answer: " + myList.get(random1 +1));
//if the user answer matches the official answer, tell them they're correct and award points - else
//tell them they are incorrect
// Display the officially correct answer from the arraylist
String correctAnswer = myList.get(random1 +1);
System.out.println("Answer: " + correctAnswer); // Instead use a variable
// if the user answer matches the official answer, tell them they're
// correct and award points
// else tell them they suck LOL
if(correctAnswer.equalsIgnoreCase(answer.trim())) {
System.out.println("Correct!");
totalScore = totalScore + awardedPoints;
System.out.println("You won " + awardedPoints);
}
else {
System.out.println("You are incorrect");
}
//display total accumulated points
System.out.println("Your total points are: " + totalScore);
//wait for user to hit any key before displaying the next question
System.out.print("Hit Enter");
System.out.println("");
scanner.nextLine();
//second question
//get category from variable, print it to user without the @ sign
System.out.println("Category: " + myList.get(cat2).substring(1));
//generate a random number in a range 1 more than the index where the category is and 2 less than
//where the next category is to get a question (always will be odd numbers)
//Credit: Derek Teay
random2 = random.nextInt(((cat3 - 2) - (cat2 + 1))) + (cat2 + 1);
//take the modulus of the random number. if there is a remainder, the number is odd, continue.
//if there is not a remainder, number is even, generate a new number until the randomly generated
//number is odd.
while (random2 % 2 == 0){
random2 = random.nextInt(((cat3 - 2) - (cat2 + 1))) + (cat2 + 1);
}
//display the question to the user
System.out.println("Question: " + myList.get(random2 + 1));
//accept user input
System.out.print("Please type your answer: ");
//store the user answer in a variable but lowercase
answer = scanner.nextLine().toLowerCase();
System.out.println();
//if the user answer matches the official answer, tell them they're correct and award points - else
//tell them they are incorrect
// Display the officially correct answer from the arraylist
String correctAnswer1 = myList.get(random2 +1);
System.out.println("Answer: " + correctAnswer1); // Instead use a variable
// if the user answer matches the official answer, tell them they're
// correct and award points
// else tell them they suck LOL
if(correctAnswer1.equalsIgnoreCase(answer.trim())) {
System.out.println("Correct!");
totalScore = totalScore + awardedPoints;
System.out.println("You won " + awardedPoints);
}
else {
System.out.println("You are wrong again");
}
//display total accumulated points
System.out.println("Your total points are: " + totalScore);
//wait for user to hit any key before displaying the next question
System.out.print("Hit Enter");
System.out.println("");
scanner.nextLine();
//third question
//get category from variable, print it to user without the @ sign
System.out.println("Category: " + myList.get(cat3).substring(1));
//generate a random number in a range 1 more than the index where the category is and 2 less than
//where the next category is to get a question (always will be odd numbers)
//Credit: Derek Teay
random3 = random.nextInt(((cat4 - 2) - (cat3 + 1))) + (cat3 + 1);
//take the modulus of the random number. if there is a remainder, the number is odd, continue.
//if there is not a remainder, number is even, generate a new number until the randomly generated
//number is odd.
while (random3 % 2 == 0){
random3 = random.nextInt(((cat4 - 2) - (cat3 + 1))) + (cat3 + 1);
}
//display the question to the user
System.out.println("Question: " + myList.get(random3 + 1));
//accept user input
System.out.print("Please type your answer: ");
//store the user answer in a variable but lowercase
answer = scanner.nextLine().toLowerCase();
System.out.println();
//if the user answer matches the official answer, tell them they're correct and award points - else
//tell them they are incorrect
// Display the officially correct answer from the arraylist
String correctAnswer2 = myList.get(random3 +1);
System.out.println("Answer: " + correctAnswer1); // Instead use a variable
// if the user answer matches the official answer, tell them they're
// correct and award points
// else tell them they suck LOL
if(correctAnswer2.equalsIgnoreCase(answer.trim())) {
System.out.println("Correct!");
totalScore = totalScore + awardedPoints;
System.out.println("You won " + awardedPoints);
}
else {
System.out.println("Wow, you really stink");
}
//display total accumulated points
System.out.println("Your total points are: " + totalScore);
//wait for user to hit any key before displaying the next question
System.out.print("Hit Enter");
System.out.println("");
scanner.nextLine();
//fourth question
//get category from variable, print it to user without the @ sign
System.out.println("Category: " + myList.get(cat4).substring(1));
//generate a random number in a range 1 more than the index where the category is and 2 less than
//where the next category is to get a question (always will be odd numbers)
//Credit: Derek Teay
random4 = random.nextInt(((cat5 - 2) - (cat4 + 1))) + (cat4 + 1);
//take the modulus of the random number. if there is a remainder, the number is odd, continue.
//if there is not a remainder, number is even, generate a new number until the randomly generated
//number is odd.
while (random4 % 2 == 0){
random4 = random.nextInt(((cat5 - 2) - (cat4 + 1))) + (cat4 + 1);
}
//display the question to the user
System.out.println("Question: " + myList.get(random4 + 1));
//accept user input
System.out.print("Please type your answer: ");
//store the user answer in a variable but lowercase
answer = scanner.nextLine().toLowerCase();
System.out.println();
//if the user answer matches the official answer, tell them they're correct and award points - else
//tell them they are incorrect
// Display the officially correct answer from the arraylist
String correctAnswer3 = myList.get(random4 +1);
System.out.println("Answer: " + correctAnswer3); // Instead use a variable
// if the user answer matches the official answer, tell them they're
// correct and award points
// else tell them they suck LOL
if(correctAnswer3.equalsIgnoreCase(answer.trim())) {
System.out.println("Correct!");
totalScore = totalScore + awardedPoints;
System.out.println("You won " + awardedPoints);
}
else {
System.out.println("You are incorrect");
}
//display total accumulated points
System.out.println("Your total points are: " + totalScore);
//wait for user to hit any key before displaying the next question
System.out.print("Hit Enter");
System.out.println("");
scanner.nextLine();
//fifth question
//get category from variable, print it to user without the @ sign
//generate a random number in a range 1 more than the index where the category is and 2 less than
//where the next category is to get a question (always will be odd numbers)
//Credit: Derek Teay
random5 = random.nextInt(((cat6 - 2) - ( cat5 + 1))) + (cat5 + 1);
//take the modulus of the random number. if there is a remainder, the number is odd, continue.
//if there is not a remainder, number is even, generate a new number until the randomly generated
//number is odd.
while (random5 % 2 == 0){
random5 = random.nextInt(((cat6 - 2) - (cat5 + 1))) + (cat5 + 1);
}
//display the question to the user
System.out.println("Question: " + myList.get(random5 + 1));
//accept user input
System.out.print("Please type your answer: ");
//store the user answer in a variable but lowercase
answer = scanner.nextLine().toLowerCase();
System.out.println();
//if the user answer matches the official answer, tell them they're correct and award points - else
//tell them they are incorrect
// Display the officially correct answer from the arraylist
String correctAnswer4 = myList.get(random5 +1);
System.out.println("Answer: " + correctAnswer4); // Instead use a variable
// if the user answer matches the official answer, tell them they're
// correct and award points
// else tell them they suck LOL
if(correctAnswer4.equalsIgnoreCase(answer.trim())) {
System.out.println("Correct!");
totalScore = totalScore + awardedPoints;
System.out.println("You won " + awardedPoints);
}
else {
System.out.println("You are incorrect");
}
//display total accumulated points
System.out.println("Your total points are: " + totalScore);
//wait for user to hit any key before displaying the next question
System.out.print("Hit Enter");
System.out.println("");
scanner.nextLine();
//sixth question
//get category from variable, print it to user without the @ sign
System.out.println("Category: " + myList.get(cat6).substring(1));
//generate a random number in a range 1 more than the index where the category is and 2 less than
//where the next category is to get a question (always will be odd numbers)
//Credit: Derek Teay
random6 = random.nextInt((cat6.maximum - cat6.minimum) + (cat6.minimum));
//take the modulus of the random number. if there is a remainder, the number is odd, continue.
//if there is not a remainder, number is even, generate a new number until the randomly generated
//number is odd.
while (random6 % 2 == 0){
random6 = random.nextInt(((cat3 - 2) - (cat2 + 1))) + (cat2 + 1);
}
//display the question to the user
System.out.println("Question: " + myList.get(random6 + 1));
//accept user input
System.out.print("Please type your answer: ");
//store the user answer in a variable but lowercase
answer = scanner.nextLine().toLowerCase();
System.out.println();
//if the user answer matches the official answer, tell them they're correct and award points - else
//tell them they are incorrect
// Display the officially correct answer from the arraylist
String correctAnswer5 = myList.get(random6 +1);
System.out.println("Answer: " + correctAnswer5); // Instead use a variable
// if the user answer matches the official answer, tell them they're
// correct and award points
// else tell them they suck LOL
if(correctAnswer1.equalsIgnoreCase(answer.trim())) {
System.out.println("Correct!");
totalScore = totalScore + awardedPoints;
System.out.println("You won " + awardedPoints);
}
else {
System.out.println("Did you even go to school?");
}
//display total accumulated points
System.out.println("Your total points are: " + totalScore);
//wait for user to hit any key before displaying the next question
System.out.print("Hit Enter");
System.out.println("");
scanner.nextLine();
}
// TODO Auto-generated method stub
}
答案 0 :(得分:0)
首先离开你的行:
random6 = random.nextInt((cat6.maximum - cat6.minimum) + (cat6.minimum));
应该是:
random6 = random.nextInt(cat6.maximum - cat6.minimum) + cat6.minimum;
您使用了大量的括号,因此很难看到,但最小值应该在之外调用nextInt()
,因为nextInt()
会返回一个值范围[0,max - min),但你想要一个在[min,max]范围内。
接下来,你可以这样做:
while ((random6 = random.nextInt(cat6.maximum - cat6.minimum) + cat6.minimum) % 2 == 0) {
continue;
}
编辑:嗯,看完你的完整代码后......
学习使用和喜爱功能。在这里,你正在做什么(根据类别索引得到一个随机问题的索引)是复杂和离散的,足以成为它自己的函数:
//generate a random number in a range 1 more than the index where the category is and 2 less than
//where the next category is to get a question (always will be odd numbers)
public static int getRandomQuestionIndex(int category) {
int min = category - 2;
int max = category + 2; // Because nextInt() is exclusive on upper bound
int random6;
while ((random6 = random.nextInt(max - min) + min) % 2 == 0) {
continue;
}
return random6;
}
答案 1 :(得分:-2)
我真的不明白你的目标是什么,但我认为这条线上有一个错误:
random6 = random.nextInt((cat6.maximum - cat6.minimum) + (cat6.minimum));
你的括号不太对劲。它应该是:
random6 = random.nextInt((cat6.maximum - cat6.minimum)) + (cat6.minimum);