0

所以,我遇到了这个程序结束的问题。投掷硬币每猜对一次得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;
}
4

1 回答 1

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;
}

而且您还在使用right1, wrong1, right2, 并且wrong2没有初始化它们。

最后,如果您要打印百分号printf()%'应该是in 。%%

于 2018-03-19T03:54:17.387 回答