因此,我在程序结束时遇到了一个问题。它应该为每一次正确猜出的掷硬币奖励10分,在100次掷骰结束时,为每位玩家提供正确答案的百分比以及总分,最后才是获胜者。我似乎有正确的百分比,但是总分的输出却是低调的,而且我不知道如何使程序使用结果来宣布获胜者。救命?

#include "stdafx.h"
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
int flip();

int main(void)
{
    int player, side, toss, turn = 1, heads = 0, tails = 0;
    int Play, wrong1, right1, wrong2, right2, r = (rand() % 2);

    puts("Oh boy.  My favorite game.  Flip the coin.  How super exciting.  I apparently am little more than a childs plaything...  \nEven though I am a program and thus do not have thumbs, hands, feet, skin or even a body, I will (reluctanly) play your silly little game, and 'flip a coin' 100 times.\nI hope you know, there are a lot better things I could be doing right now...\nReal quick, here's a list of things I could be doing right now.\n--Advance scientific research on globular clusters in the Milky Way Galaxy\n---Find the cure for cancer\n----Calculate the rate of deforestation in the Amazon\n-----Create the next generation of low emmission motor vehicles to lower the amount of CO2 in the atmosphere.\nBut, no.  Let's play 'Flip the coin'.  I couldn't possibly be more ecstatic, than if I woke this morning to see that I was on fire.  While stapled to a wall.  In New Jersey.  With Kim Kardashian holding the fire extinguisher that is actually a can of hairspray...  Yeah.\n");
    puts("Are you player 1 or player 2?\n");//question to prompt user response
    scanf_s("%d", &player);//how many players input
    puts("\nSo, let's flip a coin, because apparently this game is the 'bees-nees' of the tech world...  Enter 1 for heads, or 0 for tails.");
    srand((unsigned)time(NULL));// seed random function with current time

    for (toss = 1; toss <= 100; toss++)
    { //number of runs/tosses
        if ( player=1)
        if (turn == 1)                                                            // start of 2 player gaming
            printf("\nPlayer 1, flip the coin.  Player 2, take a guess.\n");// player 1 flip
        else
            printf("\nPlayer 2, it's your turn to flip the coin, Player 1 guess heads or tails.\n");     // player 2 flip

        if (turn == 1)
            Play = 1, turn = 0;
        else
            Play = 2, turn = 1;
        printf("\nPlayer 1 toss the coin and guess the side\n");
        int flipped;
        scanf_s("\n %d", &side);// coin guess function
        printf("\nThe coin came up %d\n", flip());

        if (side == flip() && Play == 1)
            right1++;
        else
            wrong1++;

        if (side == flip() && Play == 2)
            right2--;
        else
            wrong2++;
        if (flip() == 1)
            heads++;
        else
            tails++;
    }
    printf("heads was flipped %d times\n", heads);
    printf("tails was flipped %d times\n", tails);
    printf("Player 1 score %d \n", (right1 * 10) - (wrong1 * 5));// not working quite right player 1 eqation 1       //functions on the next 4 lines for score and confidence
    printf("Player 1 confidence %d %'\n", (right1 * 2));//percent confidence player 1   Equation 2
    printf("Player 2 score %d \n", (right2 * 10) - (wrong2 * 5));// not working quite right player 2 eqation 1
    printf("Player 2 confidence %d %'\n", (right1 * 2));//percent confidence player 2   Equation 2
    return 0;

}
int flip()// Coin flip function
{
    int i = rand() % 2;
    if (i == 0)
        return 0;
    else
        return 1;
}

最佳答案

我强烈建议正确格式化带有括号和缩进的嵌套if语句。

if ( player=1)
if (turn == 1)                                                            // start of 2 player gaming
    printf("\nPlayer 1, flip the coin.  Player 2, take a guess.\n");// player 1 flip
else
    printf("\nPlayer 2, it's your turn to flip the coin, Player 1 guess heads or tails.\n");     // player 2 flip


比阅读起来困难得多

if ( player=1) {
    if (turn == 1) {                  // start of 2 player gaming
        printf("\nPlayer 1, flip the coin.  Player 2, take a guess.\n");// player 1 flip
    } else {
        printf("\nPlayer 2, it's your turn to flip the coin, Player 1 guess heads or tails.\n");     // player 2 flip
    }
}


是的,如评论中所述

if ( player=1) {


应该

if (player == 1) {




if (turn == 1)
    Play = 1, turn = 0;
else
    Play = 2, turn = 1;


应该

if (turn == 1) {
    Play = 1;
    turn = 0;
} else {
    Play = 2;
    turn = 1;
}


而且,您还使用了right1wrong1right2wrong2而不进行初始化。

最后,在printf()中,如果要打印百分号,则%'应该为%%

10-06 05:43