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

Java实现Farkle游戏回合结束逻辑问题求助

Farkle游戏回合结束逻辑修复方案

问题根源

原代码错误地统计所有骰子中的1/5总数来判断是否有新积分,而非本次掷骰产生的新增有效点数。当用户已经锁定部分1/5骰子后,即使后续掷出的新骰子全是非1非5,count1s5s仍会因为已锁定的骰子不为0,导致循环继续执行,违反了“无新积分则回合结束且积分作废”的规则。

修复后的完整代码

import java.util.Scanner;
import java.security.SecureRandom;

public class Farkle 
{
    static int[] rolls = new int[]{0,0,0,0,0};
    static int round = 0, totalScore = 0;
    static final SecureRandom rand = new SecureRandom();
    static final Scanner in = new Scanner(System.in);

    public static void reset()
    {
        for(int i=0;i<5;i++)
            rolls[i] = 0;
    }       

    public static int calculateRoundScore()
    {
        int sum = 0;
        for(int i=0;i<5;i++)
        {
            if(rolls[i] == 1)
                sum += 100;
            if(rolls[i] == 5)
                sum += 50;
        }
        return sum;
    }

    public static void rollDice()
    {
        for(int i=0;i<5;i++)
        {
            if(rolls[i] != 1 && rolls[i] != 5)
            {
                rolls[i] = rand.nextInt(6) + 1;
            }
        }
    }

    public static void display()
    {
        for(int i=0;i<5;i++)
        {
            System.out.print(rolls[i] + "  ");
        }
        System.out.println();
    }

    public static void main(String[] args)
    {
        while(totalScore < 1000)
        {
            char ch = 'y';
            round++;
            int roundScore = 0;
            System.out.println("Beginning round " + round);
            
            do
            {
                // 掷骰前统计已锁定的1/5骰子数量
                int existingScoringDice = 0;
                for(int i=0;i<5;i++)
                {
                    if(rolls[i] == 1 || rolls[i] == 5)
                        existingScoringDice++;
                }

                System.out.printf("Your roll: ");
                rollDice();
                display();

                // 掷骰后统计所有1/5骰子数量
                int newScoringDice = 0;
                for(int i=0;i<5;i++)
                {
                    if(rolls[i] == 1 || rolls[i] == 5)
                        newScoringDice++;
                }

                // 新增数量为0 = 本次掷骰无有效点数,触发Farkle
                if(newScoringDice - existingScoringDice == 0)
                {
                    System.out.println("No new points scored; the round ends in a loss");
                    roundScore = 0; // 本轮积分作废
                    break;
                }

                roundScore = calculateRoundScore();
                System.out.printf("Current round score: %d. Keep rolling? (y/n) ", roundScore);
                ch = in.next().charAt(0);

            } while(ch != 'n'); // 用户选n或触发Farkle时结束循环

            totalScore += roundScore;
            System.out.println("Round " + round + " complete. Your current score is " + totalScore);
            reset();
        }
        in.close();
    }
}

关键修复点

  1. 新增积分判断:通过对比掷骰前后的1/5骰子数量,准确判断本次掷骰是否产生新积分,彻底解决后续掷骰误判的问题。
  2. 独立跟踪本轮积分:用roundScore单独管理本轮得分,触发Farkle时直接清零,保证本轮积分不会被计入总得分。
  3. 资源复用:将SecureRandom和Scanner改为静态实例,避免重复创建对象。
  4. 循环逻辑优化:调整do-while循环条件,明确由用户选择或无新积分两个条件终止循环,符合作业要求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 16:45:47