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

基于Java Math.random()的彩票游戏开发技术需求咨询

Java Lottery Game Implementation

Got it, let's put together this Java lottery game that meets all your requirements. I'll walk you through the code step by step, so you can follow along and tweak it as needed.

Key Requirements Recap

First, let's lock in every rule you mentioned:

  • Generate a 2-digit lottery number using Math.random()
  • Accept user input, auto-convert single-digit inputs (0-9) to 2-digit format (00-09)
  • Reward tiers:
    1. Exact order match → Php 100,000
    2. All digits match (order doesn't matter) → Php 30,000
    3. One digit matches → I'll use a common reward of Php 1,000 here—feel free to adjust this value!
    4. No matches → No reward

Complete Code

import java.util.Scanner;

public class LotteryGame {
    public static void main(String[] args) {
        // Step 1: Generate 2-digit lottery number
        int lotteryNumber = (int) (Math.random() * 100);
        String lotteryStr = String.format("%02d", lotteryNumber); // Ensures single digits become 0x format
        
        // Step 2: Get and clean up user input
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter your lottery number (1 or 2 digits works): ");
        String userInput = scanner.nextLine().trim();
        
        // Handle single-digit input by adding leading zero
        if (userInput.length() == 1) {
            userInput = String.format("%02d", Integer.parseInt(userInput));
        } else if (userInput.length() != 2) {
            System.out.println("Oops! Please enter a valid 1 or 2-digit number.");
            scanner.close();
            return;
        }
        
        // Make sure input is actually a number
        try {
            Integer.parseInt(userInput);
        } catch (NumberFormatException e) {
            System.out.println("Invalid input! Only numbers are allowed.");
            scanner.close();
            return;
        }
        
        // Step 3: Calculate reward based on matches
        int reward = 0;
        String resultMessage = "";
        
        // Check for exact order match
        if (userInput.equals(lotteryStr)) {
            reward = 100000;
            resultMessage = "Wow! You hit the jackpot—Php " + reward + " for exact match!";
        } 
        // Check for digit match (any order)
        else if (userInput.charAt(0) == lotteryStr.charAt(1) && userInput.charAt(1) == lotteryStr.charAt(0)) {
            reward = 30000;
            resultMessage = "Nice catch! You won Php " + reward + "—all digits match, just reversed!";
        } 
        // Check for single digit match
        else if (userInput.contains(String.valueOf(lotteryStr.charAt(0))) || userInput.contains(String.valueOf(lotteryStr.charAt(1)))) {
            reward = 1000;
            resultMessage = "You got one digit right! Enjoy Php " + reward + ".";
        } 
        // No matches
        else {
            resultMessage = "Sorry, no matches this time. Try again soon!";
        }
        
        // Step 4: Show final results
        System.out.println("\nLottery Number: " + lotteryStr);
        System.out.println("Your Number: " + userInput);
        System.out.println(resultMessage);
        
        scanner.close();
    }
}

Code Breakdown

Let's go over the critical parts so you understand how everything works:

  • Lottery Number Generation: (int)(Math.random() * 100) gives us a number between 0 and 99. String.format("%02d", lotteryNumber) turns single-digit numbers into two-digit strings (like 8 → "08") to keep comparisons consistent.
  • Input Handling: We automatically pad single-digit inputs with a leading zero, and add checks to reject non-numeric or overly long inputs.
  • Reward Logic:
    • Exact match: Directly compare the two 2-digit strings.
    • Reverse match: Check if the user's digits are swapped compared to the lottery number.
    • Single digit match: Use contains() to see if either lottery digit exists in the user's input.

Test Scenarios to Try

  • Exact win: Lottery is "73", user enters "73" → Php 100,000
  • Reverse win: Lottery is "73", user enters "37" → Php 30,000
  • Partial win: Lottery is "73", user enters "71" or "23" → Php 1,000
  • Single-digit input: User enters "5" → converted to "05"; if lottery is "05", you get the jackpot!
  • Invalid input: User enters "xyz" or "123" → error message pops up

Feel free to tweak reward amounts or add extra validation rules (like limiting inputs to 0-99) based on your needs!

内容的提问来源于stack exchange,提问作者Angel Lyn

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 07:48:57