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

C#文本版石头剪刀布得分达5时循环无法终止的问题

问题分析与修复方案

你的石头剪刀布游戏循环无法终止,核心原因是代码里存在得分计算错误、AI选招范围错误以及条件判断错误,导致分数无法正确累积到5,或达到5后逻辑执行异常。以下是具体问题和修复步骤:

1. AI选招范围错误

opMoves方法中random.Next(0,2)只会生成0或1,AI永远无法选择"Scissors"(数组索引为2)。Random.Next(int minValue, int maxValue)的maxValue是排他的,需修改为:

static string opMoves()
{
    return moves[random.Next(0, 3)]; // 范围改为0-2(包含2)
}

2. 得分计算逻辑错误

当玩家出Paper、AI出Scissors时,代码错误地给玩家加分,实际应给AI加分:

// 原错误代码
if (playerMove.ToLower() == "paper" && opMove == "Scissors")
{
    Console.WriteLine("Scissors beat Paper! " + opName + " gains 1 point!");
    playerScore++; // 此处错误,应改为opScore++
}

修复后:

if (playerMove.ToLower() == "paper" && opMove == "Scissors")
{
    Console.WriteLine("Scissors beat Paper! " + opName + " gains 1 point!");
    opScore++;
}

3. 平局条件判断错误

最后一个判断玩家出Scissors、AI出Scissors的条件中,错误写成opMove == "Rock",导致该平局逻辑永远不会触发:

// 原错误代码
if (playerMove.ToLower() == "scissors" && opMove == "Rock")
{
    Console.WriteLine("Scissors vs Scissors is a tie! No points awarded!");
}

修复后:

if (playerMove.ToLower() == "scissors" && opMove == "Scissors")
{
    Console.WriteLine("Scissors vs Scissors is a tie! No points awarded!");
}

4. 循环条件与输出优化

  • 循环开头的判断可简化为playerScore >=5 || opScore >=5,无需冗余逻辑
  • 原Console.WriteLine多参数写法无法正确输出变量,建议改用字符串插值语法
  • 将独立if改为else if,避免多个条件同时触发

简化后的循环核心逻辑:

while (playing)
{
    if (playerScore >= 5 || opScore >= 5)
    {
        if (playerScore >= 5)
        {
            Console.WriteLine($"Congratulations, {playerName}! You won!");
        }
        else
        {
            Console.WriteLine($"{opName} won the game! Congratulations!");
        }
        playing = false;
        break; // 直接退出循环,逻辑更稳妥
    }
    else
    {
        // 游戏主逻辑...
    }
}

完整修复后的代码

using System;
using System.Collections.Generic;

namespace Rock_Paper_Scissors
{
    class Program
    {
        static Random random = new Random();
        static string[] moves = { "Rock", "Paper", "Scissors" };
        static string opMoves()
        {
            return moves[random.Next(0, 3)];
        }

        static void Main(string[] args)
        {
            var names = new List<string> { "Hector Tucker", "Karen Adams", "Maxine Welch",
                        "May Ray", "Raymond Wood", "Leroy Newman", "Franklin Morrison",
                        "Bernice Warren", "Ron Smith", "Dave Evans", "Roosevelt Hodges",
                        "Moses James", "Noah Robertson", "Laverne Baldwin", "Leslie Tyler",
                        "Lillie Vasquez", "Tiffany Mann", "Jane Patrick", "Tracy Spencer",
                        "Kevin Wheeler"};

            int index = random.Next(0, names.Count);
            string opName = names[index];

            bool playing = true;
            int playerScore = 0;
            int opScore = 0;

            Console.WriteLine("Time to play Rock, Paper, Scissors!");
            Console.WriteLine("Please write your name below:");
            string playerName = Console.ReadLine();
            Console.WriteLine($"User:  {playerName}");
            Console.WriteLine("Choosing opponent...");
            Console.WriteLine($"Your opponent is {opName}");
            Console.WriteLine("Let's start!");

            while (playing)
            {
                if (playerScore >= 5 || opScore >= 5)
                {
                    if (playerScore >= 5)
                    {
                        Console.WriteLine($"Congratulations, {playerName}! You won!");
                    }
                    else
                    {
                        Console.WriteLine($"{opName} won the game! Congratulations!");
                    }
                    playing = false;
                    break;
                }
                else
                {
                    string opMove = opMoves();
                    Console.WriteLine($"Player Score: {playerScore}");
                    Console.WriteLine($"Opponent Score: {opScore}");
                    Console.WriteLine("Choose your move! Valid moves:\nRock\nPaper\nScissors");
                    string playerMove = Console.ReadLine();

                    Console.WriteLine($"{opName} Choosing move...");
                    Console.WriteLine($"Your move:  {playerMove}\n{opName}'s move:  {opMove}");

                    // Paper vs Rock
                    if (playerMove.ToLower() == "paper" && opMove == "Rock")
                    {
                        Console.WriteLine("Paper beats Rock! You gain 1 point!");
                        playerScore++;
                    }
                    // Paper vs Scissors
                    else if (playerMove.ToLower() == "paper" && opMove == "Scissors")
                    {
                        Console.WriteLine($"Scissors beat Paper! {opName} gains 1 point!");
                        opScore++;
                    }
                    // Paper vs Paper
                    else if (playerMove.ToLower() == "paper" && opMove == "Paper")
                    {
                        Console.WriteLine("Paper vs Paper is a tie! No points awarded!");
                    }
                    // Rock vs Paper
                    else if (playerMove.ToLower() == "rock" && opMove == "Paper")
                    {
                        Console.WriteLine($"Paper beats Rock! {opName} gains 1 point!");
                        opScore++;
                    }
                    // Rock vs Rock
                    else if (playerMove.ToLower() == "rock" && opMove == "Rock")
                    {
                        Console.WriteLine("Rock vs Rock is a tie! No points awarded!");
                    }
                    // Rock vs Scissors
                    else if (playerMove.ToLower() == "rock" && opMove == "Scissors")
                    {
                        Console.WriteLine("Rock beats Scissors! 1 point awarded to player!");
                        playerScore++;
                    }
                    // Scissors vs Paper
                    else if (playerMove.ToLower() == "scissors" && opMove == "Paper")
                    {
                        Console.WriteLine("Scissors beat Paper! 1 point awarded to player!");
                        playerScore++;
                    }
                    // Scissors vs Rock
                    else if (playerMove.ToLower() == "scissors" && opMove == "Rock")
                    {
                        Console.WriteLine($"Rock beats Scissors! {opName} gains 1 point!");
                        opScore++;
                    }
                    // Scissors vs Scissors
                    else if (playerMove.ToLower() == "scissors" && opMove == "Scissors")
                    {
                        Console.WriteLine("Scissors vs Scissors is a tie! No points awarded!");
                    }
                    else
                    {
                        Console.WriteLine("Invalid move! Please try again.");
                    }
                }
            }
        }
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 07:50:26