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

C语言掷骰子胜负统计游戏代码问题排查:执行后胜负数均为0的原因分析

Fixing Your C Dice Game: Zero Wins/Losses & Logic Issues

Alright, let's break down all the problems in your code that are causing those incorrect zero counts, plus fix the game logic to match your rules:


1. Broken Return Value & Uninitialized Variables

  • First off, your rolldice function tries to return two values with return wins, losses;—C doesn't support this! The comma operator here only returns the last value (losses), and you're not even capturing that return value in main anyway.
  • Worse, wins and losses aren't initialized to 0—they start with random garbage values, which would mess up counting even if the rest worked.
  • Fix: Use pointer parameters to pass the addresses of your wins and losses variables from main to rolldice, so the function can modify them directly.

2. Early Return Kills the Loop

Your return statement is inside the for loop—this means the function exits after the first iteration, so you never run 1000 games. Move any return logic outside the loop (or use void since we're using pointers now).

3. Game Logic Is Completely Misaligned with Rules

Your current code treats num[0] (the first roll of the first game) as the "target" for all 1000 games, which is wrong. Each game is independent:

  • For each game, you need to first roll once, check if it's 7 (win immediately).
  • If not, keep rolling until you either match that first roll (win) or roll a 7 (lose).

4. Random Seed Placement

srand(time(NULL)) should only be called once at the start of your program. Putting it inside rolldice could cause duplicate random numbers if the function is called multiple times quickly.


Corrected Code

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

// Use pointers to modify the win/loss counts in main
void rolldice(int *wins, int *losses) {
    // Initialize counts to zero
    *wins = 0;
    *losses = 0;

    for (int game = 0; game < 1000; game++) {
        // First roll of the current game
        int first_roll = (rand() % 6 + 1) + (rand() % 6 + 1);

        if (first_roll == 7) {
            // Win immediately on first roll 7
            (*wins)++;
            continue; // Jump to next game
        }

        // Keep rolling until win/lose condition is met
        while (1) {
            int current_roll = (rand() % 6 + 1) + (rand() % 6 + 1);
            if (current_roll == first_roll) {
                // Matched first roll: win
                (*wins)++;
                break;
            } else if (current_roll == 7) {
                // Rolled 7 before matching first roll: lose
                (*losses)++;
                break;
            }
            // Else, keep rolling
        }
    }
}

int main() {
    int wins, losses;
    // Initialize random seed ONCE at program start
    srand(time(NULL));
    // Pass addresses of wins/losses to the function
    rolldice(&wins, &losses);
    printf("wins: %d \nlosses: %d\n", wins, losses);
    return 0;
}

Key Changes Explained:

  • Pointer Parameters: rolldice takes int *wins and int *losses so it can update the variables in main directly.
  • Per-Game Logic: Each game has its own first roll, and we loop until that game's win/lose condition is hit.
  • Proper Initialization: Counts start at 0, no garbage values.
  • Single Random Seed: srand is called once in main for proper randomness.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 17:58:16