You need to enable JavaScript to run this app. Java石头剪刀布游戏:如何实现先得3胜终止的循环逻辑?
优惠活动
大模型
产品
解决方案
定价
更多

Java石头剪刀布游戏:如何实现先得3胜终止的循环逻辑?

石头剪刀布循环对战功能修改指南

核心错误定位

  • 循环条件逻辑反转:原代码写的while(userScore == 3)仅在用户已拿到3胜时才触发循环,和需求完全相反,正确逻辑是双方胜场均未达到3时,持续开启新对局。
  • 循环位置非法:原代码将循环块写在RockPaperScissors类的闭合大括号外侧,Java编译器无法识别,根本不会执行。
  • 单局逻辑未嵌套进循环:原本的用户输入、电脑选招、胜负判定、计分逻辑全部位于循环外,仅执行一次就终止程序。
  • 冗余代码:原声明的scoreCounter变量全程未使用,无存在必要。

关键修改点

  • 新增条件为userScore < 3 && computerScore < 3的while循环,只要任意一方胜场均未到3,就持续跑新对局,一旦某方达成3胜立刻退出循环。
  • 将全部单局对战逻辑(用户输入、电脑随机生成选择、调用判定方法、胜负处理、比分打印)移入循环块内,每轮循环对应一局对战。
  • 删除了类外的非法循环代码、未使用的scoreCounter变量。
  • 循环退出后新增最终全局胜负判定逻辑,直接输出比赛结果。
  • 合并了重复的比分打印逻辑,每轮对战结束后统一输出当前比分,减少代码冗余。

修改后的完整可运行代码

import java.util.Scanner;
import java.util.Random;
    
public class RockPaperScissors
{
    public static void main(String[] args)
    {
        Scanner key;
        Random randGen;
        char userChoice, computerChoice;
        int winner, compInt;

        int computerScore;
        int userScore;

        /***** INITIALIZATION SECTION *****/
        key = new Scanner(System.in);
        randGen = new Random();
        userChoice = ' ';
        computerChoice = ' ';
        compInt = -1;
        computerScore = 0;
        userScore = 0;

        System.out.println("What is your name?");
        String name = key.nextLine();
        System.out.println(name + " play rock, paper, or scissors with me!!\n");

        // 循环对战:任意一方拿到3胜即退出
        while (userScore < 3 && computerScore < 3) {
            /***** INPUT SECTION *****/
            System.out.println("Please enter R for rock, P for paper, or S for scissors:");
            userChoice = key.next().toUpperCase().charAt(0);

            /***** PROCESSING SECTION *****/
            compInt = randGen.nextInt(3);
            if (compInt == 0)
            {
                computerChoice = 'R';
            }
            else if (compInt == 1)
            {
                computerChoice = 'P';
            }
            else if (compInt == 2)
            {
                computerChoice = 'S';
            }
            
            winner = RockPaperScissors.decideWinner(userChoice, computerChoice);

            /***** OUTPUT SECTION *****/
            System.out.printf("Player: %c%nComputer: %c%n", userChoice, computerChoice);

            switch (winner)
            {
                case 0:
                    System.out.println("It's a tie! No points added.");
                    break;
                case 1:
                    System.out.println("You won this round!");
                    userScore++;
                    break;
                case 2:
                    System.out.println("Computer won this round!");
                    computerScore++;
                    break;
                default:
                    System.out.println("Invalid choice, no points added. Try again.");
                    break;
            }
            // 每轮结束打印当前比分
            System.out.println("Current score - " + name + ": " + userScore + ", Computer: " + computerScore + "\n");
        }

        // 输出最终全局结果
        if (userScore == 3) {
            System.out.println("Final Result: You won the game " + name + "! Congratulations");
        } else {
            System.out.println("Final Result: Sorry " + name + ", Computer won the game...");
        }
        key.close();
    }

    /***** STATIC METHODS *****/
    /**
     * Description: given two characters (R,P, or S) determines winner using rock
     * paper rules. Assume input is valid and error checking is done in main
     * programme. 
     **/
    public static int decideWinner(char p1, char p2)
    {
        String combo;
        combo = String.format("%c%c", p1, p2);

        switch(combo)
        {
            case "RS":
            case "PR":
            case "SP":
                return 1;
            case "RP":
            case "PS":
            case "SR":
                return 2;
            case "RR":
            case "PP":
            case "SS":
                return 0;
        }
        return -1;
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.22 14:06:02