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

C语言石头剪刀布游戏while循环故障排查求助

石头剪刀布游戏代码修复(针对循环及逻辑问题)

你的代码存在多个关键问题导致循环无法正常工作,以下是问题梳理及修复方案:

核心问题清单

  • while循环条件错误:使用逗号表达式life1 > 0, life2 > 0,C语言中逗号表达式仅取最后一个表达式的值,实际只会判断life2 > 0,无法实现"双方都有生命值时继续"的逻辑。
  • 玩家选择逻辑位置错误:输入和选择判断代码在循环外,仅执行一次,导致循环重复同一结果。
  • result变量逻辑失效:多次重复赋值后仅保留最后一个固定值,永远无法匹配后续的胜负判断条件。
  • 错误输入处理混乱:错误输入直接将生命值设为-1,导致循环无法正常终止。
  • 胜负判定脱离实际选择:当前result计算完全与玩家输入无关,无法正确判定单轮胜负。

修复后的完整代码

#include <stdio.h>

int main() {
    // 用整数替代浮点数,避免精度比较问题
    int life1 = 3, life2 = 3;
    int choice1, choice2;

    // 循环条件:双方生命值均大于0时继续游戏
    while (life1 > 0 && life2 > 0) {
        // 玩家1输入与验证
        printf("Player 1, choose (1=Paper, 2=Scissors, 3=Rock): ");
        scanf("%d", &choice1);
        while (choice1 != 1 && choice1 != 2 && choice1 != 3) {
            printf("Invalid input! Choose again (1=Paper, 2=Scissors, 3=Rock): ");
            scanf("%d", &choice1);
        }
        switch(choice1) {
            case 1: printf("Player 1 chose Paper\n"); break;
            case 2: printf("Player 1 chose Scissors\n"); break;
            case 3: printf("Player 1 chose Rock\n"); break;
        }

        // 玩家2输入与验证
        printf("Player 2, choose (-1=Paper, -2=Scissors, -3=Rock): ");
        scanf("%d", &choice2);
        while (choice2 != -1 && choice2 != -2 && choice2 != -3) {
            printf("Invalid input! Choose again (-1=Paper, -2=Scissors, -3=Rock): ");
            scanf("%d", &choice2);
        }
        switch(choice2) {
            case -1: printf("Player 2 chose Paper\n"); break;
            case -2: printf("Player 2 chose Scissors\n"); break;
            case -3: printf("Player 2 chose Rock\n"); break;
        }

        // 单轮胜负判定
        if ((choice1 == 1 && choice2 == -1) || (choice1 == 2 && choice2 == -2) || (choice1 == 3 && choice2 == -3)) {
            printf("This round is a draw! No life lost.\n");
        } 
        else if ((choice1 == 1 && choice2 == -3) || (choice1 == 2 && choice2 == -1) || (choice1 == 3 && choice2 == -2)) {
            life2--;
            printf("Player 1 wins this round! Player 2 has %d lives left.\n", life2);
        }
        else {
            life1--;
            printf("Player 2 wins this round! Player 1 has %d lives left.\n", life1);
        }
        printf("------------------------\n");
    }

    // 最终胜负判定
    if (life1 == 0) {
        printf("Player 1 has no lives left! Player 2 wins the game!\n");
    } else {
        printf("Player 2 has no lives left! Player 1 wins the game!\n");
    }

    return 0;
}

关键修改说明

  1. 循环条件修正:改为life1 > 0 && life2 > 0,确保双方都有生命值时才继续游戏。
  2. 输入逻辑内移:将玩家输入、选择验证的代码放到循环内部,每轮游戏都重新选择。
  3. 类型优化:用整数替代浮点数,避免浮点数比较的精度误差。
  4. 输入验证增强:错误输入时提示重新输入,而非直接终止游戏逻辑。
  5. 胜负逻辑简化:直接根据玩家选择判断单轮胜负,去掉冗余的result变量,逻辑更清晰。
  6. 体验提升:每轮结束后显示剩余生命值,游戏流程更直观。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 13:15:24