硬币投掷游戏程序求助:分数输出异常与胜负判定实现问题
Hey there! Let's work through your coin toss game issues—sounds like you're almost done, just a couple of kinks to iron out.
Fixing the Garbled Total Score Output
The most common reasons for garbled score output are uninitialized variables or mismatched format specifiers in your printf calls. Here's what to check:
- Initialize your score variables: If you declared
player1_scoreandplayer2_scorebut didn't set them to0at the start, they'll hold random garbage values from memory, which look like "garbage" when printed. Always start scores at 0!int player1_score = 0; int player2_score = 0; - Use the right format specifier: When printing integers like scores, use
%dinprintf. If you accidentally used%s(for strings) or another specifier, that'll cause weird output. For example:// Correct way to print score printf("Total Score: %d\n", player1_score); - Double-check the scoring logic: Make sure you're actually adding 10 points every time a guess is correct. A typo like
player1_score =+ 10(instead of+=) would break this—easy mistake to make!
Adding Win/Loss Determination
To decide the winner, you can compare total scores first (since each correct guess gives equal points), and fall back to accuracy if scores are tied. Here's a straightforward way to implement this:
// After calculating scores and accuracy if (player1_score > player2_score) { printf("Player 1 takes the win!\n"); } else if (player2_score > player1_score) { printf("Player 2 is the champion!\n"); } else { // Scores are tied—check accuracy if (player1_accuracy > player2_accuracy) { printf("Player 1 wins by higher accuracy!\n"); } else if (player2_accuracy > player1_accuracy) { printf("Player 2 wins by higher accuracy!\n"); } else { printf("It's a perfect tie—great game!\n"); } }
Full Corrected Code Snippet
Here's a polished version of your code with these fixes included (I filled in some missing parts like random toss generation and player guess logic):
#include "stdafx.h" #include<stdio.h> #include<stdlib.h> #include<time.h> int main() { const int TOTAL_TOSSES = 100; int player1_correct = 0, player2_correct = 0; int player1_score = 0, player2_score = 0; // Initialized scores float player1_accuracy, player2_accuracy; // Seed random number generator for realistic tosses srand(time(0)); for (int i = 0; i < TOTAL_TOSSES; i++) { // Simulate coin toss (0 = tails, 1 = heads) int toss_result = rand() % 2; // Replace this with actual user input if needed int player1_guess = rand() % 2; int player2_guess = rand() % 2; // Update correct counts and scores if (player1_guess == toss_result) { player1_correct++; player1_score += 10; } if (player2_guess == toss_result) { player2_correct++; player2_score += 10; } } // Calculate accuracy (cast to float to avoid integer division) player1_accuracy = ((float)player1_correct / TOTAL_TOSSES) * 100; player2_accuracy = ((float)player2_correct / TOTAL_TOSSES) * 100; // Print results printf("=== Player 1 Results ===\n"); printf("Correct Guesses: %d\n", player1_correct); printf("Accuracy: %.2f%%\n", player1_accuracy); printf("Total Score: %d\n\n", player1_score); printf("=== Player 2 Results ===\n"); printf("Correct Guesses: %d\n", player2_correct); printf("Accuracy: %.2f%%\n", player2_accuracy); printf("Total Score: %d\n\n", player2_score); // Determine winner if (player1_score > player2_score) { printf("Player 1 wins!\n"); } else if (player2_score > player1_score) { printf("Player 2 wins!\n"); } else { if (player1_accuracy > player2_accuracy) { printf("Player 1 wins by higher accuracy!\n"); } else if (player2_accuracy > player1_accuracy) { printf("Player 2 wins by higher accuracy!\n"); } else { printf("It's a tie!\n"); } } return 0; }
Just swap out the random guesses with actual user input using scanf if that's part of your program, and this should work smoothly.
内容的提问来源于stack exchange,提问作者Will Swartz

