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

C语言石头剪刀布程序异常:输入选择后为何重复请求输入?

问题分析与修复方案

核心问题

程序陷入无限请求输入的循环,根源是整数与字符的错误比较:

  • computer_choice是整数类型,rand() % 3生成的是0、1、2这三个整数
  • 但你在条件判断里把它和字符'0'、'1'、'2'比较(比如computer_choice == '0'),整数0和字符'0'的ASCII值(48)不相等,导致所有胜负判断的条件都无法触发
  • 最终player_score和computer_score永远达不到3,do-while循环的终止条件player_score < 3 && computer_score < 3一直为真,程序持续重复请求输入

修复步骤

  1. 修正数值比较逻辑:把所有条件中的'0'、'1'、'2'改成整数0、1、2,比如将computer_choice == '0'改为computer_choice == 0,所有类似判断都要修改。
  2. 添加非法输入处理(推荐):当前程序未处理非r/p/s的输入,可在所有条件后加else分支提示输入错误,避免无效输入导致的循环。
  3. 修复分数显示逻辑:原代码仅在双方分数都不为0时显示分数,改为只要有一方有分数就显示,更符合游戏逻辑。
  4. 重置分数(隐藏问题修复):新增游戏重启时的分数重置逻辑,避免下一局延续上一局的分数。

修复后的完整代码

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
    int player_score = 0, computer_score = 0;
    char player_choice2;

    printf("\nWELCOME TO ROCK PAPER SCISSORS !\n\nFirst to get three points wins\n\n");

    srand(time(NULL));

    for (;;) {
        do {
            char player_choice;
            int computer_choice = rand() % 3;

            // 修复分数显示条件
            if (player_score > 0 || computer_score > 0) {
                printf("Your score: %d\nComputer's score: %d\n\n", player_score, computer_score);
            }

            printf("Choose between r for rock, p for paper and s for scissors\n\n");
            scanf(" %c", &player_choice);

            // 修正整数与字符的比较
            if (player_choice == 'r' && computer_choice == 0) {
                printf("Rock! It's a tie.\n\n");
            } else if (player_choice == 'p' && computer_choice == 0) {
                printf("Rock!\n\n");
                player_score += 1;
            } else if (player_choice == 's' && computer_choice == 0) {
                printf("Rock!\n\n");
                computer_score += 1;
            } else if (player_choice == 'r' && computer_choice == 1) {
                printf("Paper!\n\n");
                computer_score += 1;
            } else if (player_choice == 'p' && computer_choice == 1) {
                printf("Paper! It's a tie.\n\n");
            } else if (player_choice == 's' && computer_choice == 1) {
                printf("Paper!\n\n");
                player_score += 1;
            } else if (player_choice == 'r' && computer_choice == 2) {
                printf("Scissors!\n\n");
                player_score += 1;
            } else if (player_choice == 'p' && computer_choice == 2) {
                printf("Scissors!\n\n");
                computer_score += 1;
            } else if (player_choice == 's' && computer_choice == 2) {
                printf("Scissors! It's a tie.\n\n");
            } else {
                // 添加非法输入提示
                printf("Invalid input! Please enter r, p or s.\n\n");
            }

        } while (player_score < 3 && computer_score < 3);

        if (player_score == 3) {
            printf("Congratulations, you won !\n\n");
        } else {
            printf("Sorry, you lost !\n\n");
        }

        printf("Do you wish to play again ? Enter Y for yes or N for no\n");
        scanf(" %c", &player_choice2);
        if (player_choice2 == 'N') {
            break;
        }
        // 重置分数,准备下一局
        player_score = 0;
        computer_score = 0;
    }
    return 0;
}

内容的提问来源于stack exchange,提问作者Clément

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 12:47:06