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

Java版石头剪刀布程序输出不符预期问题排查

Java石头剪刀布程序问题排查与修复

核心问题分析

  • 循环逻辑完全错误:原代码使用for (int i = 0; i <= numOfRounds+1; ++i),导致循环次数为numOfRounds+2次(输入15回合时会循环17次),完全不符合“执行指定数量有效回合”的要求。
  • 平局处理不符合需求:题目要求平局时重新生成随机值(该平局不算入有效回合),但原代码仅输出“Tie”就完成当前循环迭代,将平局计入了循环次数,导致最终有效回合数与指定回合数不符。

修正后的代码

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

public class LabProgram {
    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        final int ROCK = 0;
        final int PAPER = 1;
        final int SCISSORS = 2;
        Random rand = new Random();
        int seed = scnr.nextInt();
        rand.setSeed(seed);

        String player1;
        String player2;
        int numOfRounds;

        player1 = scnr.next();
        player2 = scnr.next();
        numOfRounds = scnr.nextInt();

        while (numOfRounds < 1) {
            System.out.println("Rounds must be > 0");
            numOfRounds = scnr.nextInt();
        }
        System.out.println(player1 + " vs " + player2 + " for " + numOfRounds + " rounds");

        int p1wins = 0;
        int p2wins = 0;

        // 用计数器确保完成指定数量的非平局有效回合
        int validRoundCount = 0;
        while (validRoundCount < numOfRounds) {
            int p1 = rand.nextInt(3);
            int p2 = rand.nextInt(3);
            if (p1 == p2) {
                System.out.println("Tie");
                // 平局不计数,直接重新生成
                continue;
            }
            // 分出胜负,有效回合数加1
            validRoundCount++;

            if (p1 == 0 && p2 == 1) {
                System.out.println(player2 + " wins with paper");
                p2wins += 1;
            } else if (p1 == 0 && p2 == 2) {
                System.out.println(player1 + " wins with rock");
                p1wins += 1;
            } else if (p1 == 1 && p2 == 0) {
                System.out.println(player1 + " wins with paper");
                p1wins += 1;
            } else if (p1 == 1 && p2 == 2) {
                System.out.println(player2 + " wins with scissors");
                p2wins += 1;
            } else if (p1 == 2 && p2 == 0) {
                System.out.println(player2 + " wins with rock");
                p2wins += 1;
            } else if (p1 == 2 && p2 == 1) {
                System.out.println(player1 + " wins with scissors");
                p1wins += 1;
            }
        }

        // 补充总战绩为平局的输出逻辑
        if (p1wins < p2wins) {
            System.out.println(player2 + " wins " + p2wins + " and " + player1 + " wins " + p1wins);
        } else if (p2wins < p1wins) {
            System.out.println(player1 + " wins " + p1wins + " and " + player2 + " wins " + p2wins);
        } else {
            System.out.println("Overall tie! " + player1 + " wins " + p1wins + ", " + player2 + " wins " + p2wins);
        }
    }
}

修正说明

  • 替换错误的for循环为while循环,通过validRoundCount计数器严格控制有效回合数,确保完成指定数量的非平局对局。
  • 平局时使用continue跳过后续逻辑,不增加有效回合数,直接重新生成随机值,符合题目要求。
  • 补充了总战绩为平局时的输出,完善程序完整性。

验证结果

输入17 Randy Jenny 15后,程序会生成符合预期的输出:完成15次有效回合(9胜6负),平局会输出但不计入有效回合数,最终总战绩与预期一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 15:43:09