Who Wants To Be A Millionaire Java: Game
while (true) System.out.print("\nEnter choice (A/B/C/D), 'W' to walk away, 'L' for lifelines: "); String input = scanner.nextLine().trim().toUpperCase(); if (input.equals("W")) System.out.println("You walked away with $" + getGuaranteedPrize()); System.exit(0); else if (input.equals("L")) !audienceUsed) System.out.println("Available lifelines:"); if (!fiftyUsed) System.out.println("1 - 50:50"); if (!phoneUsed) System.out.println("2 - Phone a Friend"); if (!audienceUsed) System.out.println("3 - Ask the Audience"); System.out.print("Choose lifeline number (or 0 to cancel): "); int lChoice = scanner.nextInt(); scanner.nextLine(); if (lChoice == 1 && !fiftyUsed) useFiftyFifty(q); else if (lChoice == 2 && !phoneUsed) usePhoneAFriend(q); else if (lChoice == 3 && !audienceUsed) useAskAudience(q); else System.out.println("Invalid or already used."); else System.out.println("No lifelines left!"); continue; else int answerIndex = -1; switch (input) case "A": answerIndex = 0; break; case "B": answerIndex = 1; break; case "C": answerIndex = 2; break; case "D": answerIndex = 3; break; default: System.out.println("Invalid input."); continue; if (answerIndex == q.correctOption) System.out.println("\n✅ Correct! You've won $" + PRIZES[currentLevel - 1]); return true; else System.out.println("\n❌ Wrong! The correct answer was " + (char)('A' + q.correctOption) + ": " + q.options[q.correctOption]); return false;
public class MillionaireGame private static final int[] PRIZES = 100, 200, 300, 500, 1000, // levels 1-5 2000, 4000, 8000, 16000, 32000, // levels 6-10 64000, 125000, 250000, 500000, 1000000 // levels 11-15 ; private static final int SAFE_LEVEL_1 = 5; // $1,000 private static final int SAFE_LEVEL_2 = 10; // $32,000
public void start() System.out.println("====================================="); System.out.println(" WHO WANTS TO BE A MILLIONAIRE?"); System.out.println("====================================="); System.out.println("Rules: Answer 15 questions. Use lifelines wisely."); System.out.println("Walk away anytime by typing 'W'.\n");
private boolean askQuestion(Question q) System.out.println("\n" + q.text); for (int i = 0; i < 4; i++) System.out.println((char) ('A' + i) + ": " + q.options[i]); who wants to be a millionaire java game
private void loadQuestions() questions = new Question[15]; // Level 1 questions[0] = new Question("What is the capital of France?", new String[]"Berlin", "Madrid", "Paris", "Lisbon", 2, 1); // Level 2 questions[1] = new Question("Which planet is known as the Red Planet?", new String[]"Mars", "Jupiter", "Venus", "Saturn", 0, 2); // Level 3 questions[2] = new Question("Who painted the Mona Lisa?", new String[]"Van Gogh", "Picasso", "Da Vinci", "Rembrandt", 2, 3); // Level 4 questions[3] = new Question("What is the largest ocean on Earth?", new String[]"Atlantic", "Indian", "Arctic", "Pacific", 3, 4); // Level 5 questions[4] = new Question("Which year did World War II end?", new String[]"1943", "1944", "1945", "1946", 2, 5); // Level 6 questions[5] = new Question("Who wrote 'Romeo and Juliet'?", new String[]"Charles Dickens", "Jane Austen", "Mark Twain", "William Shakespeare", 3, 6); // Level 7 questions[6] = new Question("What is the square root of 144?", new String[]"10", "11", "12", "13", 2, 7); // Level 8 questions[7] = new Question("Which element has the chemical symbol 'O'?", new String[]"Gold", "Oxygen", "Osmium", "Silver", 1, 8); // Level 9 questions[8] = new Question("Who directed the movie 'Jurassic Park'?", new String[]"James Cameron", "Steven Spielberg", "George Lucas", "Ridley Scott", 1, 9); // Level 10 questions[9] = new Question("Which country gifted the Statue of Liberty to the USA?", new String[]"England", "Spain", "France", "Germany", 2, 10); // Level 11 questions[10] = new Question("What is the hardest natural substance?", new String[]"Iron", "Gold", "Diamond", "Platinum", 2, 11); // Level 12 questions[11] = new Question("Who discovered penicillin?", new String[]"Marie Curie", "Alexander Fleming", "Louis Pasteur", "Isaac Newton", 1, 12); // Level 13 questions[12] = new Question("Which city hosted the 2016 Summer Olympics?", new String[]"London", "Rio de Janeiro", "Tokyo", "Beijing", 1, 13); // Level 14 questions[13] = new Question("What is the longest river in the world?", new String[]"Amazon", "Nile", "Yangtze", "Mississippi", 1, 14); // Level 15 questions[14] = new Question("Who painted the Sistine Chapel ceiling?", new String[]"Leonardo da Vinci", "Raphael", "Donatello", "Michelangelo", 3, 15);
while (currentLevel <= 15) displayPrizeLadder(); Question currentQ = questions[currentLevel - 1]; boolean correct = askQuestion(currentQ);
private void usePhoneAFriend(Question q) System.out.println("\n*** PHONE A FRIEND ***"); String[] hints = "I think it's B but not sure.", "My gut says C.", "I'm leaning towards A.", "Definitely not D!", "I remember it's C." ; int hintIndex = random.nextInt(hints.length); System.out.println("Your friend says: \"" + hints[hintIndex] + "\""); phoneUsed = true; while (true) System
public MillionaireGame() scanner = new Scanner(System.in); random = new Random(); currentLevel = 1; fiftyUsed = false; phoneUsed = false; audienceUsed = false; loadQuestions();
private void displayPrizeLadder() System.out.println("\n===== PRIZE LADDER ====="); for (int i = 0; i < PRIZES.length; i++) if (i + 1 == currentLevel) System.out.printf("-> Level %2d: $%,d (current)\n", i + 1, PRIZES[i]); else System.out.printf(" Level %2d: $%,d\n", i + 1, PRIZES[i]); System.out.println("========================\n");
if (correct) if (currentLevel == 15) System.out.println("\n🎉🎉🎉 CONGRATULATIONS! YOU ARE A MILLIONAIRE! 🎉🎉🎉"); System.out.println("You win $1,000,000!"); break; currentLevel++; else int prize = getGuaranteedPrize(); System.out.println("\nGame over! You leave with $" + prize); break; scanner.close(); Use lifelines wisely
Enter choice (A/B/C/D), 'W' to walk away, 'L' for lifelines: C
private int getGuaranteedPrize() if (currentLevel > SAFE_LEVEL_2) return PRIZES[SAFE_LEVEL_2 - 1]; else if (currentLevel > SAFE_LEVEL_1) return PRIZES[SAFE_LEVEL_1 - 1]; else return 0;
public static void main(String[] args) MillionaireGame game = new MillionaireGame(); game.start();
public Question(String text, String[] options, int correctOption, int level) this.text = text; this.options = options; this.correctOption = correctOption; this.level = level;