You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java新手编写保险箱破解游戏:三位随机数及猜数逻辑疑问

Fixing & Completing Your Safe-Cracking Game

Hey there! Great job getting started with your safe-cracking game—you’ve laid a solid foundation with the random code generation. Let’s walk through the missing pieces and fix small issues to make the game fully functional.

First, Fix the Random Code Generation

Your takeRandomSafeCode method is almost perfect, but there’s a tiny bug right now: when you use random.nextInt(max - min) + min, you’ll never get the max value (999 in your case). That’s because nextInt(n) returns a number from 0 to n-1. To include 999, adjust the method like this:

private static int takeRandomSafeCode(int min, int max) { 
    Random random = new Random(); 
    // Add +1 to max-min to include the upper bound
    int result = random.nextInt(max - min + 1) + min; 
    return result;
}

Next, Implement the takeGuess() Method

You’ve referenced this method but haven’t written it yet. We need a way to get valid 3-digit input from the console, even if the user enters something invalid. Here’s a robust version:

private static int takeGuess() {
    Scanner scanner = new Scanner(System.in);
    int guess = -1;
    while (guess < 100 || guess > 999) {
        System.out.print("Enter your 3-digit guess: ");
        try {
            guess = scanner.nextInt();
            if (guess < 100 || guess > 999) {
                System.out.println("Please enter a valid 3-digit number (100-999)!");
            }
        } catch (InputMismatchException e) {
            System.out.println("Oops, that's not a number. Try again!");
            scanner.next(); // Clear the invalid input from the scanner
        }
    }
    return guess;
}

Don’t forget to import java.util.Scanner and java.util.InputMismatchException at the top of your class!

Build the Full Game Loop in playGame()

Right now, your playGame() only gets the safe code and one guess—we need to let the user keep guessing until they get it right, with feedback each time. Update the method like this:

private static void playGame() { 
    int safeCode = takeRandomSafeCode(100, 999); 
    int guess;
    boolean isCracked = false;

    System.out.println("Welcome to the Safe Cracking Game!");
    System.out.println("Try to guess the 3-digit safe code.\n");

    while (!isCracked) {
        guess = takeGuess();
        if (guess == safeCode) {
            System.out.println("\nCongratulations! You cracked the safe! The code was " + safeCode);
            isCracked = true;
        } else if (guess > safeCode) {
            System.out.println("Too high! Try a lower number.\n");
        } else {
            System.out.println("Too low! Try a higher number.\n");
        }
    }
}

Put It All Together in a Main Class

Finally, add a main method to launch the game:

import java.util.Random;
import java.util.Scanner;
import java.util.InputMismatchException;

public class SafeCrackerGame {
    public static void main(String[] args) {
        playGame();
    }

    // Paste all the methods we wrote above here: takeRandomSafeCode, takeGuess, playGame
}

That’s it! Now your game will generate a valid 3-digit code, prompt for guesses, give clear feedback, and keep running until the safe is cracked.

内容的提问来源于stack exchange,提问作者Eren Arican

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:07:04