嗨,我是java的初学者,我正试图编写这个想成为百万富翁游戏的人。我已经设置了一个方法和数组,所以程序应该每次重复不同的问题和不同的答案。但是,questions
方法只运行一次,即使我在main方法中调用了几次。谢谢。
//WWTBAM Adam Lam
import java.util.Scanner;
import java.util.Random;
class WWTBAMGOOD {
static Random random = new Random();
static Scanner scanner = new Scanner(System.in);
static int randomnum;
static int rand=100;
static int sum = 0;
static String lifeline1= "1";
static String lifeline2="2";
static String lifeline3="3";
static String a = "a";
static String b = "b";
static String c = "c";
static String d = "d";
static String guess = "0";
static String name;
static String finalanswer = "0";
static String x = "yes";
static String y = "no";
static int num1 = -1;
static int num2 = -1;
static int num3=-1;
static int num4=-1;
static String[] answerkey = { "a", "b", "c", "d", "a", "b", "c", "d", "a", "b", "c", "d", "a", "b", "c", "d", "a", "b", "c", "d", "a", "b", "c", "d", };
static String[] allquestions1 = { "What does NBA most commonly stand for? \n A. National Basketball Association \n B. New Brunswick Accounting \n C. Nevada Bowling Association \n D. Nile Boating Associates", "Which country invented hockey? \n A. Iceland \n B. Canada \n C. Australia \n D. China", "What is the point of soccer? \n A. To find a sock \n B. To get to the other side \n C. To score a ball into the opposing team's net \n D. To see who's the fastest runnner" , "In parts of Europe, North American's soccer is more commonnly called what? \n A. Kicker \n B. European Ball \n C. Tennis Golf \n D. Football" , "Which of these is a hockey player? \n A. Wayne Gretzky \n B. Babe Ruth \n C. Steph Curry \n D. Michael Jordan" };
static void questions(){
do{
int randomnum = random.nextInt(4);
} while(rand==randomnum);
rand = randomnum;
System.out.println("Remember, you can always use your lifelines. Press 1 for 50/50, 2 for Poll the audience, and 3 for call a friend");
do{
do{
System.out.println (allquestions1[randomnum]);
guess=scanner.nextLine();
if ((!guess.equalsIgnoreCase(a) && !guess.equalsIgnoreCase(b) && !guess.equalsIgnoreCase(c) && !guess.equalsIgnoreCase(d)&&!guess.equalsIgnoreCase(lifeline1)&&!guess.equalsIgnoreCase(lifeline2)&&!guess.equalsIgnoreCase(lifeline3))) {
System.out.println("Invalid Input. Please Try Again. \n\n");
}
} while(!guess.equalsIgnoreCase(a) && !guess.equalsIgnoreCase(b) && !guess.equalsIgnoreCase(c) && !guess.equalsIgnoreCase(d)&&!guess.equalsIgnoreCase(lifeline1)&&!guess.equalsIgnoreCase(lifeline2)&&!guess.equalsIgnoreCase(lifeline3));
System.out.println("Is this your final answer? (yes or no)");
finalanswer=scanner.nextLine();
} while (!finalanswer.equalsIgnoreCase(x));
if (guess.equalsIgnoreCase(answerkey[randomnum])) {
System.out.println("You got it right! Congrats!");
}
else if ((guess.equalsIgnoreCase(a) || (guess.equalsIgnoreCase(b)) || (guess.equalsIgnoreCase(c)) || (guess.equalsIgnoreCase(d)))) {
System.out.println("Sorry you got it wrong :( \n Better luck next time");
}
while(sum<=1){
if (guess.equals(lifeline1)){
System.out.println("You chose the 50/50 lifeline!");
do{
do{
System.out.println("Your choices are now between option " + (answerkey[randomnum]) + " and " + ((answerkey [randomnum+1])));
guess = scanner.nextLine();
if ((!guess.equalsIgnoreCase(a) && !guess.equalsIgnoreCase(b) && !guess.equalsIgnoreCase(c) && !guess.equalsIgnoreCase(d)&&!guess.equalsIgnoreCase(lifeline1)&&!guess.equalsIgnoreCase(lifeline2)&&!guess.equalsIgnoreCase(lifeline3))) {
System.out.println("Invalid Input. Please Try Again. \n\n");
}
} while(!guess.equalsIgnoreCase(a) && !guess.equalsIgnoreCase(b) && !guess.equalsIgnoreCase(c) && !guess.equalsIgnoreCase(d)&&!guess.equalsIgnoreCase(lifeline1)&&!guess.equalsIgnoreCase(lifeline2)&&!guess.equalsIgnoreCase(lifeline3));
System.out.println("Is this your final answer? (yes or no)");
finalanswer=scanner.nextLine();
} while(!finalanswer.equals(x));
sum++;
if (guess.equals(answerkey[randomnum])) {
System.out.println("You got it right!!! Congrats " + name+ " !! You won 100 Dollars!!! \n\n");
}
else {
System.out.println("Sorry you got it wrong :(");
}
}
}
while (sum<=1) {
if (guess.equalsIgnoreCase(lifeline2)) {
System.out.println("You chose the poll the audience lifeline!");
num1 = random.nextInt(10)+80;
num2 = random.nextInt(6)+1;
num3 = random.nextInt(5)+1;
num4 = 100 - (num1+num2+num3);
do{
do{
System.out.println("The audience, out of 100 people said: \n A. National Basketball Association: "+ (num1)+ " \n B. New Brunswick Accounting: "+num2+ "\n C. Nevada Bowling Association: "+num3+" \n D. Nile River Boating: "+num4 + "\n\n Enter your new guess:");
guess = scanner.nextLine();
if (!guess.equalsIgnoreCase(a) && !guess.equalsIgnoreCase(b) && !guess.equalsIgnoreCase(c) && !guess.equalsIgnoreCase(d)&&!guess.equalsIgnoreCase(lifeline1)&& !guess.equalsIgnoreCase(lifeline2)&& !guess.equalsIgnoreCase(lifeline3)) {
System.out.println("Invalid Input. Please Try Again. \n\n");
}
} while(!guess.equalsIgnoreCase(a) && !guess.equalsIgnoreCase(b) && !guess.equalsIgnoreCase(c) && !guess.equalsIgnoreCase(d)&&!guess.equalsIgnoreCase(lifeline1)&& !guess.equalsIgnoreCase(lifeline2)&& !guess.equalsIgnoreCase(lifeline3));
System.out.println("Is this your final answer? (yes or no)");
finalanswer=scanner.nextLine();
} while(!finalanswer.equals(x));
sum++;
if (guess.equals(answerkey[randomnum])) {
System.out.println("You got it right!!! Congrats " + name+ " !! \n\n");
}
else {
System.out.println("Sorry you got it wrong :(");
}
}
}
while(sum<=1)
if(guess.equalsIgnoreCase(lifeline3)) {
System.out.println("You chose the call a friend lifeline!");
do{
System.out.println("Your friend says: Oh it's definitely A! I grew up watching the National Basketball Association and playing basketball!");
do{
System.out.println("What is your answer?");
guess = scanner.nextLine();
if ((!guess.equalsIgnoreCase(a) && !guess.equalsIgnoreCase(b) && !guess.equalsIgnoreCase(c) && !guess.equalsIgnoreCase(d)&& !guess.equalsIgnoreCase(lifeline1)&& !guess.equalsIgnoreCase(lifeline2)&&!guess.equalsIgnoreCase(lifeline3))) {
System.out.println("Invalid Input. Please Try Again. \n\n");
}
} while(!guess.equalsIgnoreCase(a) &&!guess.equalsIgnoreCase(b)&& !guess.equalsIgnoreCase(c) && !guess.equalsIgnoreCase(d)&& !guess.equalsIgnoreCase(lifeline1)&& !guess.equalsIgnoreCase(lifeline2)&& !guess.equalsIgnoreCase(lifeline3));
System.out.println("Is this your final answer? (yes or no)");
finalanswer=scanner.nextLine();
} while(!finalanswer.equals(x));
sum++;
if (guess.equals(answerkey[randomnum])) {
System.out.println("You got it right!!! Congrats " + name+ " !! \n\n");
}
else {
System.out.println("Sorry you got it wrong :(");
}
}
}
public static void main (String [] args ) {
System.out.println("What's your name?");
name = scanner.nextLine();
System.out.println("Welcome to Who Wants to be a Millionaire! As I'm sure you know, you will have to answer 13 questions correctly in a row. \n But you have 3 lifelines to work with: 50/50, poll the audience, or ask a friend \n So, " + name+ " lets begin! You have a chance to win ONE MILLION DOLLARS!!! \n\n\n\n\n");
questions();
System.out.println("You are now at 100 Dollars!!!!");
questions();
questions();
questions();
questions();
}
}