如何让问题不重复
How to make Questions not repeat itself
所以我正在制作一个谁想成为百万富翁的游戏代码,并且我的一切都运行良好。但有时问题会重复出现,如果您最终两次得到相同的问题,那么这不是一款好游戏。我将不胜感激所有的帮助。
import java.io.*;
import java.util.*;
public class MillionaireGame {
public static void main(String[] args) {
// gets the users name
Scanner kbreader = new Scanner(System.in);
System.out.println("Hello welcome to Wants to Be A Millionaire, Can i get your name please.");
String name;
name=kbreader.next();
// introduce the host and have the host explain the rules
System.out.println("Welcome again " + name +" I am your host Antonio. i'll be here with you while you play.");
System.out.println("I am here to explain the rules to you and be a host. let me tell you the rules of the game.");
System.out.println("We will ask you a series of questions 14 in total and you win money if you get the questions right");
System.out.println("There two check points 000 and 0000. if you lose or quit before 000 you go home empty handed.");
System.out.println("If you lose or quit after the 000 question but before the 0000 you go home with 000.");
System.out.println("If you lose or quit after the 0000 question but before the 00000 you go home with 0000.");
System.out.println("Let the games being computer first question please.");
//The string array is to how the possible question one
String question1[]={"What year did world war 2 end in",
"The canadian great depression started in",
"Sir John A Mcdonald last term started in",
"Canada became its own contruy in",};
//these string arrays are to keep the asnswer for the questions and give the other options
String answer1[]={"1945","1929","1891","1867",};
String options1[]={"1943","1904","1886","1782","1748","1764","1897","1823","1899","1858","1900","1909",};
String question2[]={"What is the capital of Yukon",
"What is the capital of Northwest Territories",
"Where is Parliament Hill",
"_________ is called No Fun City "};
String answer2[]={"Whitehorse","Yellowknife","Ottawa","Vancouver"};
String options2[]={"London","Kingston","Saint John","Winnipeg","Victoria","Edmonton","Calgary","Montreal"};
//The string array is to how the possible question one
String question3[]={"Which country has the most people",
"Which country invented football(soccer)",
"Which country started World War one",
"Which country has the largest debt",};
//these string arrays are to keep the asnswer for the questions and give the other options
String answer3[]={"China","England","Austria-Hungary","United States",};
String options3[]={"Canada","India","Japan","Germany","Russia","Brazil","France","Mexio","Egypt","Jamaica","Italy","Austria",};
//The string array is to how the possible question one
String question4[]={"To which Super-Hero was With great power comes Great Responsibility said to",
"Which Super-Hero takes the role of Captain America after he died",
"What is the name of the Acher on the Avengers",
"Other than Captian America, Chris Evans acted as which Super-Hero",};
//these string arrays are to keep the asnswer for the questions and give the other options
String answer4[]={"Spider-Man","Winter Soldier","Hawkeye","Human Torch",};
String options4[]={"Super-Man","Batman","Flash","Iron Man","Hulk","Thor","DareDevil","Green Arrow","QuickSliver","Super-Girl","Nova","StarLord",};
//This int array is to hold the prices for the money and keep count of it as the play gets higher
int money[]={0,1000,2000,3000,5000,10000,20000,40000,80000,160000,320000,640000,1250000,2500000,5000000,10000000};
//This string array is to talk to the user and give them the next question
String com1[]={"Antonio: Coumpter what is next question",
"Antonio: Next question please",
"Antonio: Are you ready for the next question",
"Antonio : Next question Please"};
String com2[]={"Antonio: We will tell you what the answer was",
"Antonio: Since you quit you get to know the answer",
"Antonio: Since you're dying to know what the answer was",
"Antonio: The computer will show you the answer"};
//this random is to get pick which questions answers and options are printed
Random r= new Random();
int qch,ra,rq=0,ans=0,nextq=0,m=1,bal=0;
do{
System.out.println("3");
qch=r.nextInt(4);
int len1=options1.length;
int len2=options2.length;
int len3=options3.length;
int len4=options4.length;
System.out.println("\nyou have won= "+money[bal]+"\n");
System.out.println("\n\tThis question is for $"+money[m]+"\n");
//if statements to matcht he answer with the question even after it was randomize
//the if statements also print out the options
if(qch==0)
{
rq=r.nextInt(4);
System.out.println(question1[rq]);
ra=r.nextInt(4);
if(ra==0)
{
System.out.println(answer1[rq]);
for(int i=0;i<3;i++)
System.out.println(options1[r.nextInt(len1)]);
ans=1;
}
if(ra==1)
{
System.out.println(options1[r.nextInt(len1)]);
System.out.println(answer1[rq]);
for(int i=0;i<2;i++)
System.out.println(options1[r.nextInt(len1)]);
ans=2;
}
if(ra==2)
{
for(int i=0;i<2;i++)
System.out.println(options1[r.nextInt(len1)]);
System.out.println(answer1[rq]);
System.out.println(options1[r.nextInt(len1)]);
ans=3;
}
if(ra==3)
{
for(int i=0;i<3;i++)
System.out.println(options1[r.nextInt(len1)]);
System.out.println(answer1[rq]);
ans=4;
}
}
if(qch==1)
{
rq=r.nextInt(4);
System.out.println(question2[rq]);
ra=r.nextInt(4);
if(ra==0)
{
System.out.println(answer2[rq]);
for(int i=0;i<3;i++)
System.out.println(options2[r.nextInt(len2)]);
ans=1;
}
if(ra==1)
{
System.out.println(options2[r.nextInt(len2)]);
System.out.println(answer2[rq]);
for(int i=0;i<2;i++)
System.out.println(options2[r.nextInt(len2)]);
ans=2;
}
if(ra==2)
{
for(int i=0;i<2;i++)
System.out.println(options2[r.nextInt(len2)]);
System.out.println(answer2[rq]);
System.out.println(options2[r.nextInt(len2)]);
ans=3;
}
if(ra==3)
{
for(int i=0;i<3;i++)
System.out.println(options2[r.nextInt(len2)]);
System.out.println(answer2[rq]);
ans=4;
}
}
if(qch==2)
{
rq=r.nextInt(4);
System.out.println(question3[rq]);
ra=r.nextInt(4);
if(ra==0)
{
System.out.println(answer3[rq]);
for(int i=0;i<3;i++)
System.out.println(options3[r.nextInt(len3)]);
ans=1;
}
if(ra==1)
{
System.out.println(options3[r.nextInt(len3)]);
System.out.println(answer3[rq]);
for(int i=0;i<2;i++)
System.out.println(options3[r.nextInt(len3)]);
ans=2;
}
if(ra==2)
{
for(int i=0;i<2;i++)
System.out.println(options3[r.nextInt(len3)]);
System.out.println(answer3[rq]);
System.out.println(options3[r.nextInt(len3)]);
ans=3;
}
if(ra==3)
{
for(int i=0;i<3;i++)
System.out.println(options3[r.nextInt(len3)]);
System.out.println(answer3[rq]);
ans=4;
}
}
if(qch==3)
{
rq=r.nextInt(4);
System.out.println(question4[rq]);
ra=r.nextInt(4);
if(ra==0)
{
System.out.println(answer4[rq]);
for(int i=0;i<3;i++)
System.out.println(options4[r.nextInt(len4)]);
ans=1;
}
if(ra==1)
{
System.out.println(options4[r.nextInt(len4)]);
System.out.println(answer4[rq]);
for(int i=0;i<2;i++)
System.out.println(options4[r.nextInt(len4)]);
ans=2;
}
if(ra==2)
{
for(int i=0;i<2;i++)
System.out.println(options4[r.nextInt(len4)]);
System.out.println(answer4[rq]);
System.out.println(options4[r.nextInt(len4)]);
ans=3;
}
if(ra==3)
{
for(int i=0;i<3;i++)
System.out.println(options4[r.nextInt(len4)]);
System.out.println(answer4[rq]);
ans=4;
}
}
//The Scanner to get input from the user
//Made a second scanner to save as the answer from the user
Scanner sc=new Scanner(System.in);
int uans;
System.out.println("\n\t\tenter your answer 1,2,3,4");
System.out.println("\n\t\tif you want to quit enter 5");
uans=sc.nextInt();
if(uans==ans)
{
System.out.println("\n\t\t\tCorrect answer\n");
nextq=1;m++;bal++;
//if they won the whole game this is printed out
if(m==14)
{
System.out.println("\nCongrats\t\tCongrats");
System.out.println("\n\t\t You Won A $" + m);
System.out.println("\nCongrats\t\tCongrats");
}
}
// if the user chose to quit the game this is printed
else if(uans==5)
{
System.out.println("\t\t\tYou have chose to quit the game");
System.out.println(com2[r.nextInt(4)]);
System.out.print("\t\tThe answer to the question was ");
if(qch==0)
System.out.println("option "+ans+" "+answer1[rq]);
if(qch==1)
System.out.println("option "+ans+" "+answer2[rq]);
if(qch==2)
System.out.println("option "+ans+" "+answer3[rq]);
nextq=0;
if(qch==3)
System.out.println("option "+ans+" "+answer4[rq]);
nextq=0;
}
//if the user chose the wrong answer to the Question this is printed
else
{
nextq=0;
System.out.println("\n\t Sorry that is the wrong answer");
System.out.print("\t\tThe answer to the question was ");
if(qch==0)
System.out.println("option "+ans+" "+answer1[rq]);
if(qch==1)
System.out.println("option "+ans+" "+answer2[rq]);
if(qch==2)
System.out.println("option "+ans+" "+answer3[rq]);
nextq=0;
if(qch==3)
System.out.println("option "+ans+" "+answer4[rq]);
nextq=0;
System.out.print("\t\t\t You have won $");
//Checks how much money the user already has then prints out how much they won
if((money[bal]<10000)&&(money[bal]>=0))
System.out.println("0");
if((money[bal]<320000)&&(money[bal]>=10000))
System.out.println("10000");
if((money[bal]<10000000)&&(money[bal]>=320000))
System.out.println("320000");
}
if(nextq==1)
System.out.println(com1[r.nextInt(4)]);
}while(nextq==1);
}
}
跟踪 ArrayList
中已经提出了哪些问题(或者,您可以使用任何一种可用的存储方法),并检查该列表以查看您所关心的问题问之前已经问过。如果是这样,请问一个不同的问题。否则,提出问题。
我不会为您调试大量代码转储,而且我怀疑我们中的许多人会这样做。请阅读 How to Ask 以增加获得更好答案的机会。
不要在第一个问题问的时候使用随机数。生成随机数列表,在提出任何问题之前将在其中提出问题。
使用同时保持随机序列和唯一性的LinkedHashSet。
Set<Integer> generated = new
LinkedHashSet<Integer>();
while (generated.size() < numbersNeeded)
{
Integer next = rng.nextInt(max) + 1;
generated.add(next);
}
这种事情的一个选择是创建一个包含所有问题的 List
,使用 Collections.shuffle()
随机打乱它,然后从中创建一个队列。然后你就可以从你的队列中弹出问题,你永远不会重复一个问题。可能比跟踪提出了哪些问题更容易。
下面是一个使用简单整数列表执行此操作的示例:
package test;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Queue;
public class Test {
public static void main(String[] argv) {
List<Integer> intList = Arrays.asList(1,2,3,4,5);
Collections.shuffle(intList);
Queue<Integer> intQueue = new ArrayDeque<Integer>(intList);
Integer x;
while ((x = intQueue.poll()) != null) {
System.out.println(x);
}
}
}
所以我正在制作一个谁想成为百万富翁的游戏代码,并且我的一切都运行良好。但有时问题会重复出现,如果您最终两次得到相同的问题,那么这不是一款好游戏。我将不胜感激所有的帮助。
import java.io.*;
import java.util.*;
public class MillionaireGame {
public static void main(String[] args) {
// gets the users name
Scanner kbreader = new Scanner(System.in);
System.out.println("Hello welcome to Wants to Be A Millionaire, Can i get your name please.");
String name;
name=kbreader.next();
// introduce the host and have the host explain the rules
System.out.println("Welcome again " + name +" I am your host Antonio. i'll be here with you while you play.");
System.out.println("I am here to explain the rules to you and be a host. let me tell you the rules of the game.");
System.out.println("We will ask you a series of questions 14 in total and you win money if you get the questions right");
System.out.println("There two check points 000 and 0000. if you lose or quit before 000 you go home empty handed.");
System.out.println("If you lose or quit after the 000 question but before the 0000 you go home with 000.");
System.out.println("If you lose or quit after the 0000 question but before the 00000 you go home with 0000.");
System.out.println("Let the games being computer first question please.");
//The string array is to how the possible question one
String question1[]={"What year did world war 2 end in",
"The canadian great depression started in",
"Sir John A Mcdonald last term started in",
"Canada became its own contruy in",};
//these string arrays are to keep the asnswer for the questions and give the other options
String answer1[]={"1945","1929","1891","1867",};
String options1[]={"1943","1904","1886","1782","1748","1764","1897","1823","1899","1858","1900","1909",};
String question2[]={"What is the capital of Yukon",
"What is the capital of Northwest Territories",
"Where is Parliament Hill",
"_________ is called No Fun City "};
String answer2[]={"Whitehorse","Yellowknife","Ottawa","Vancouver"};
String options2[]={"London","Kingston","Saint John","Winnipeg","Victoria","Edmonton","Calgary","Montreal"};
//The string array is to how the possible question one
String question3[]={"Which country has the most people",
"Which country invented football(soccer)",
"Which country started World War one",
"Which country has the largest debt",};
//these string arrays are to keep the asnswer for the questions and give the other options
String answer3[]={"China","England","Austria-Hungary","United States",};
String options3[]={"Canada","India","Japan","Germany","Russia","Brazil","France","Mexio","Egypt","Jamaica","Italy","Austria",};
//The string array is to how the possible question one
String question4[]={"To which Super-Hero was With great power comes Great Responsibility said to",
"Which Super-Hero takes the role of Captain America after he died",
"What is the name of the Acher on the Avengers",
"Other than Captian America, Chris Evans acted as which Super-Hero",};
//these string arrays are to keep the asnswer for the questions and give the other options
String answer4[]={"Spider-Man","Winter Soldier","Hawkeye","Human Torch",};
String options4[]={"Super-Man","Batman","Flash","Iron Man","Hulk","Thor","DareDevil","Green Arrow","QuickSliver","Super-Girl","Nova","StarLord",};
//This int array is to hold the prices for the money and keep count of it as the play gets higher
int money[]={0,1000,2000,3000,5000,10000,20000,40000,80000,160000,320000,640000,1250000,2500000,5000000,10000000};
//This string array is to talk to the user and give them the next question
String com1[]={"Antonio: Coumpter what is next question",
"Antonio: Next question please",
"Antonio: Are you ready for the next question",
"Antonio : Next question Please"};
String com2[]={"Antonio: We will tell you what the answer was",
"Antonio: Since you quit you get to know the answer",
"Antonio: Since you're dying to know what the answer was",
"Antonio: The computer will show you the answer"};
//this random is to get pick which questions answers and options are printed
Random r= new Random();
int qch,ra,rq=0,ans=0,nextq=0,m=1,bal=0;
do{
System.out.println("3");
qch=r.nextInt(4);
int len1=options1.length;
int len2=options2.length;
int len3=options3.length;
int len4=options4.length;
System.out.println("\nyou have won= "+money[bal]+"\n");
System.out.println("\n\tThis question is for $"+money[m]+"\n");
//if statements to matcht he answer with the question even after it was randomize
//the if statements also print out the options
if(qch==0)
{
rq=r.nextInt(4);
System.out.println(question1[rq]);
ra=r.nextInt(4);
if(ra==0)
{
System.out.println(answer1[rq]);
for(int i=0;i<3;i++)
System.out.println(options1[r.nextInt(len1)]);
ans=1;
}
if(ra==1)
{
System.out.println(options1[r.nextInt(len1)]);
System.out.println(answer1[rq]);
for(int i=0;i<2;i++)
System.out.println(options1[r.nextInt(len1)]);
ans=2;
}
if(ra==2)
{
for(int i=0;i<2;i++)
System.out.println(options1[r.nextInt(len1)]);
System.out.println(answer1[rq]);
System.out.println(options1[r.nextInt(len1)]);
ans=3;
}
if(ra==3)
{
for(int i=0;i<3;i++)
System.out.println(options1[r.nextInt(len1)]);
System.out.println(answer1[rq]);
ans=4;
}
}
if(qch==1)
{
rq=r.nextInt(4);
System.out.println(question2[rq]);
ra=r.nextInt(4);
if(ra==0)
{
System.out.println(answer2[rq]);
for(int i=0;i<3;i++)
System.out.println(options2[r.nextInt(len2)]);
ans=1;
}
if(ra==1)
{
System.out.println(options2[r.nextInt(len2)]);
System.out.println(answer2[rq]);
for(int i=0;i<2;i++)
System.out.println(options2[r.nextInt(len2)]);
ans=2;
}
if(ra==2)
{
for(int i=0;i<2;i++)
System.out.println(options2[r.nextInt(len2)]);
System.out.println(answer2[rq]);
System.out.println(options2[r.nextInt(len2)]);
ans=3;
}
if(ra==3)
{
for(int i=0;i<3;i++)
System.out.println(options2[r.nextInt(len2)]);
System.out.println(answer2[rq]);
ans=4;
}
}
if(qch==2)
{
rq=r.nextInt(4);
System.out.println(question3[rq]);
ra=r.nextInt(4);
if(ra==0)
{
System.out.println(answer3[rq]);
for(int i=0;i<3;i++)
System.out.println(options3[r.nextInt(len3)]);
ans=1;
}
if(ra==1)
{
System.out.println(options3[r.nextInt(len3)]);
System.out.println(answer3[rq]);
for(int i=0;i<2;i++)
System.out.println(options3[r.nextInt(len3)]);
ans=2;
}
if(ra==2)
{
for(int i=0;i<2;i++)
System.out.println(options3[r.nextInt(len3)]);
System.out.println(answer3[rq]);
System.out.println(options3[r.nextInt(len3)]);
ans=3;
}
if(ra==3)
{
for(int i=0;i<3;i++)
System.out.println(options3[r.nextInt(len3)]);
System.out.println(answer3[rq]);
ans=4;
}
}
if(qch==3)
{
rq=r.nextInt(4);
System.out.println(question4[rq]);
ra=r.nextInt(4);
if(ra==0)
{
System.out.println(answer4[rq]);
for(int i=0;i<3;i++)
System.out.println(options4[r.nextInt(len4)]);
ans=1;
}
if(ra==1)
{
System.out.println(options4[r.nextInt(len4)]);
System.out.println(answer4[rq]);
for(int i=0;i<2;i++)
System.out.println(options4[r.nextInt(len4)]);
ans=2;
}
if(ra==2)
{
for(int i=0;i<2;i++)
System.out.println(options4[r.nextInt(len4)]);
System.out.println(answer4[rq]);
System.out.println(options4[r.nextInt(len4)]);
ans=3;
}
if(ra==3)
{
for(int i=0;i<3;i++)
System.out.println(options4[r.nextInt(len4)]);
System.out.println(answer4[rq]);
ans=4;
}
}
//The Scanner to get input from the user
//Made a second scanner to save as the answer from the user
Scanner sc=new Scanner(System.in);
int uans;
System.out.println("\n\t\tenter your answer 1,2,3,4");
System.out.println("\n\t\tif you want to quit enter 5");
uans=sc.nextInt();
if(uans==ans)
{
System.out.println("\n\t\t\tCorrect answer\n");
nextq=1;m++;bal++;
//if they won the whole game this is printed out
if(m==14)
{
System.out.println("\nCongrats\t\tCongrats");
System.out.println("\n\t\t You Won A $" + m);
System.out.println("\nCongrats\t\tCongrats");
}
}
// if the user chose to quit the game this is printed
else if(uans==5)
{
System.out.println("\t\t\tYou have chose to quit the game");
System.out.println(com2[r.nextInt(4)]);
System.out.print("\t\tThe answer to the question was ");
if(qch==0)
System.out.println("option "+ans+" "+answer1[rq]);
if(qch==1)
System.out.println("option "+ans+" "+answer2[rq]);
if(qch==2)
System.out.println("option "+ans+" "+answer3[rq]);
nextq=0;
if(qch==3)
System.out.println("option "+ans+" "+answer4[rq]);
nextq=0;
}
//if the user chose the wrong answer to the Question this is printed
else
{
nextq=0;
System.out.println("\n\t Sorry that is the wrong answer");
System.out.print("\t\tThe answer to the question was ");
if(qch==0)
System.out.println("option "+ans+" "+answer1[rq]);
if(qch==1)
System.out.println("option "+ans+" "+answer2[rq]);
if(qch==2)
System.out.println("option "+ans+" "+answer3[rq]);
nextq=0;
if(qch==3)
System.out.println("option "+ans+" "+answer4[rq]);
nextq=0;
System.out.print("\t\t\t You have won $");
//Checks how much money the user already has then prints out how much they won
if((money[bal]<10000)&&(money[bal]>=0))
System.out.println("0");
if((money[bal]<320000)&&(money[bal]>=10000))
System.out.println("10000");
if((money[bal]<10000000)&&(money[bal]>=320000))
System.out.println("320000");
}
if(nextq==1)
System.out.println(com1[r.nextInt(4)]);
}while(nextq==1);
}
}
跟踪 ArrayList
中已经提出了哪些问题(或者,您可以使用任何一种可用的存储方法),并检查该列表以查看您所关心的问题问之前已经问过。如果是这样,请问一个不同的问题。否则,提出问题。
我不会为您调试大量代码转储,而且我怀疑我们中的许多人会这样做。请阅读 How to Ask 以增加获得更好答案的机会。
不要在第一个问题问的时候使用随机数。生成随机数列表,在提出任何问题之前将在其中提出问题。
使用同时保持随机序列和唯一性的LinkedHashSet。
Set<Integer> generated = new
LinkedHashSet<Integer>();
while (generated.size() < numbersNeeded)
{
Integer next = rng.nextInt(max) + 1;
generated.add(next);
}
这种事情的一个选择是创建一个包含所有问题的 List
,使用 Collections.shuffle()
随机打乱它,然后从中创建一个队列。然后你就可以从你的队列中弹出问题,你永远不会重复一个问题。可能比跟踪提出了哪些问题更容易。
下面是一个使用简单整数列表执行此操作的示例:
package test;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Queue;
public class Test {
public static void main(String[] argv) {
List<Integer> intList = Arrays.asList(1,2,3,4,5);
Collections.shuffle(intList);
Queue<Integer> intQueue = new ArrayDeque<Integer>(intList);
Integer x;
while ((x = intQueue.poll()) != null) {
System.out.println(x);
}
}
}