gpt4 book ai didi

c - 宣布抛硬币游戏的获胜者

转载 作者:行者123 更新时间:2023-11-30 14:49:47 25 4
gpt4 key购买 nike

所以,我在该程序的结束时遇到了问题。每掷正确一次,奖励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() 中,如果您尝试打印百分号,%' 应该是 %%

关于c - 宣布抛硬币游戏的获胜者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49355503/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com