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

C语言石头剪刀布程序退出时计分板显示异常求助

Troubleshooting Scoreboard Reset Issue in Rock-Paper-Scissors C Program

Hey there! No worries at all about being new to programming—everyone hits these little snags when starting out. Let’s fix that scoreboard problem where your scores reset to 0 when you exit the game.

Common Cause: Variable Scope

The most likely culprit here is variable scope. If you defined your score variables (like player_wins, computer_wins, ties) inside a function (not main()), those variables are local to that function. That means every time you call the function, those variables get reinitialized to 0, and any changes you make to them don’t stick outside the function.

Example of the Problem

Say your code looks something like this:

// Game function with local score variables
void play_game() {
    int player_wins = 0, computer_wins = 0, ties = 0;
    // ... game logic that increments these scores ...
}

int main() {
    play_game();
    // When you exit, trying to print player_wins here won't work—it doesn't exist in main's scope!
    // Or if you have another function to print scores, it might be using its own local 0 values
    return 0;
}

In this case, the scores only exist inside play_game() and get discarded once the function ends.

A clean way to keep track of scores is to define them in main() and pass pointers to them into your game functions. This lets the functions modify the original variables in main() directly.

Here’s how to adjust your code:

#include <stdio.h>

// Function prototype: takes pointers to score variables
void play_game(int *player_wins, int *computer_wins, int *ties);
void print_scores(int p_wins, int c_wins, int ties);

int main() {
    // Define scores in main—these will persist through the program
    int player_wins = 0, computer_wins = 0, ties = 0;
    
    play_game(&player_wins, &computer_wins, &ties);
    print_scores(player_wins, computer_wins, ties); // Now this will show correct values!
    
    return 0;
}

void play_game(int *player_wins, int *computer_wins, int *ties) {
    char choice;
    // Example game loop
    for(;;) {
        printf("Enter your choice (R/P/S/Q to quit): ");
        scanf(" %c", &choice);
        
        if(choice == 'Q' || choice == 'q') {
            break;
        }
        
        // ... your game logic to determine win/lose/tie ...
        // When player wins:
        *player_wins += 1; // Use * to modify the original variable via pointer
        // When computer wins:
        *computer_wins += 1;
        // When tie:
        *ties += 1;
    }
}

void print_scores(int p_wins, int c_wins, int ties) {
    printf("\nFinal Scores:\n");
    printf("Player: %d\n", p_wins);
    printf("Computer: %d\n", c_wins);
    printf("Ties: %d\n", ties);
}

Fix 2: Use Global Variables (Simpler for Beginners, Less Flexible Long-Term)

If pointers feel overwhelming right now, you can define your score variables as global (outside any function). Global variables exist for the entire program’s runtime, so their values won’t reset unless you explicitly set them to 0.

#include <stdio.h>

// Global score variables—accessible by all functions
int player_wins = 0, computer_wins = 0, ties = 0;

void play_game();
void print_scores();

int main() {
    play_game();
    print_scores();
    return 0;
}

void play_game() {
    char choice;
    for(;;) {
        printf("Enter your choice (R/P/S/Q to quit): ");
        scanf(" %c", &choice);
        
        if(choice == 'Q' || choice == 'q') {
            break;
        }
        
        // ... game logic ...
        player_wins += 1; // Directly modify global variable
        // ... etc. ...
    }
}

void print_scores() {
    printf("\nFinal Scores:\n");
    printf("Player: %d\n", player_wins);
    printf("Computer: %d\n", computer_wins);
    printf("Ties: %d\n", ties);
}

Quick Checks to Rule Out Other Issues

  • Double-check that you’re not accidentally setting the scores back to 0 right before printing them (like a stray player_wins = 0; in your exit logic).
  • Make sure your score increment logic is actually running when a win/tie happens (add a debug print, e.g., printf("Player won! Score: %d\n", player_wins); after incrementing).

You’ve got this—variable scope is a common hurdle for new C programmers, and once you wrap your head around it, these problems will get easier to spot!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 10:17:27