gpt4 book ai didi

c - C 新手,类似二十一点的程序的错误消息问题

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

所以我在类里面编写一个类似于二十一点的程序,但出现以下错误:

ThirtyNine.c:27:54: error: expected expression before 'int'
(playerdie1,playerdie2,playerdie3) = int initial3roll();
^
ThirtyNine.c:28:60: error: expected expression before 'int'
(opponentdie1,opponentdie2,opponentdie3) = int initial3roll();
^
ThirtyNine.c:29:60: error: expected ')' before string constant
printf("You roll 3 dice: ", playerdie1 " ", playerdie2, " ", playerdie3);
^
ThirtyNine.c:29:60: warning: too many arguments for format [-Wformat-extra-args]
ThirtyNine.c:30:69: error: expected ')' before string constant
printf("Opponent rolls 3 dice: ", (opponentdie1 " ", opponentdie2, " ", opponentdie3);
^
ThirtyNine.c:30:106: error: expected ')' before ';' token
printf("Opponent rolls 3 dice: ", (opponentdie1 " ", opponentdie2, " ", opponentdie3);
^
ThirtyNine.c:92:37: warning: too many arguments for format [-Wformat-extra-args]
}
^
ThirtyNine.c:92:37: error: expected ';' before '}' token

这是我到目前为止的代码,任何帮助将不胜感激!

int initial3roll();
int main(){
int playerdie1,playerdie2,playerdie3;
int opponentdie1,opponentdie2,opponentdie3;
int playertotal;
int opponenttotal;
int select1;
int diceroll;
printf("Welcome to the game of 39!\n Press 1 to start or 0 to exit:");
scanf( "%d", &select1 );

if ( select1 == 0) {
exit(0);
}
else {
int initial3roll();{
int die1 = 1 + (rand() % 12);
int die2 = 1 + (rand() % 12);
int die3 = 1 + (rand() % 12);
return (die1,die2,die3);
}
while (select1 == 1){
(playerdie1,playerdie2,playerdie3) = int initial3roll();
(opponentdie1,opponentdie2,opponentdie3) = int initial3roll();
printf("You roll 3 dice: ", playerdie1 " ", playerdie2, " ", playerdie3);
printf("Opponent rolls 3 dice: ", (opponentdie1 " ", opponentdie2, " ", opponentdie3);
playertotal = playerdie1 + playerdie2 + playerdie3;
opponenttotal = opponentdie1 + opponentdie2 + opponentdie3;
printf("Your total: ", playertotal);
printf("Opponent total: ", opponenttotal);
printf("How many dice do you want to roll again: ");
scanf("&d", &diceroll );
switch(diceroll){
case '1' int die1 = 1 + (rand() % 12);
printf("You roll 1 die: ", die1);
playertotal += die1;
die1 = 1 + (rand() % 12);
printf("Opponent rolls 1 die: ", die1);
opponenttotal += die1;
printf("Your total is: ", playertotal);
printf("Opponent total is: ", opponenttotal);
break;
case '2'
int die1 = 1 + (rand() % 12);
int die2 = 1 + (rand() % 12);
printf("You roll 2 die: ", die1, die2);
playertotal += die1 + die2;
die1 = 1 + (rand() % 12);
die2 = 1 + (rand() % 12);
printf("Opponent rolls 2 die: " die1, die2);
opponenttotal += die1 + die2;
printf("Your total is: ", playertotal);
printf("Opponent total is: ", opponenttotal);
break;
case '3'
int die1 = 1 + (rand() % 12);
int die2 = 1 + (rand() % 12);
int die3 = 1 + (rand() % 12);
printf("You roll 3 die: ", die1, die2,die3);
playertotal += die1 + die2 + die3;
die1 = 1 + (rand() % 12);
die2 = 1 + (rand() % 12);
die3 = 1 + (rand() % 12);
printf("Opponent rolls 3 die: " die1, die2, die3);
opponenttotal += die1 + die2 + die3;
printf("Your total is: ", playertotal);
printf("Opponent total is: ", opponenttotal);
break;
default:
printf("Your total is: ", playertotal);
printf("Opponent total is: ", opponenttotal);
break;
}
if (playertotal > opponenttotal);
printf("You Win!");
if (opponenttotal > playertotal);
printf("You Lose");
if (opponenttotal = playertotal);
printf("You Tie");


printf("Press 1 to play again or 0 to exit:");
scanf( "%d", &select1 );
if (select1 == 0){
printf("Thanks for playing!");
exit(0);
}
}

}
return(0);

}

最佳答案

一些突出的事情:

1) 了解如何使用 printf -- 如果您是 C 语言新手,情况会非常不同。您可以将单个字符串作为第一个参数,将参数列表作为其余参数,然后使用 % 后跟类型标识符来表示字符串中的参数。例如: %d 表示 int,因此:

printf("You roll 3 dice: %d %d %d", playerdie1, playerdie2, playerdie3);

2) C没有嵌套函数;您需要将 intinitial3roll() 的定义单独移到 main 之外。它应该类似于您对 main 的定义。

3) C 没有元组,所以这不起作用:

(playerdie1,playerdie2,playerdie3) = int initial3roll();

除非您想使用指针或传入引用参数,否则您必须从 initial3roll 返回单个 int。您可能需要考虑只运行此函数 3 次,每次滚动一次,以简化操作。或者干脆将其完全删除并单独使用 rand

4) 每个case 都需要在数字后加一个冒号,并且可能应该独占一行(为了清楚起见)。 default 大小写是正确的。

5)让事情变得更容易,并正确缩进你的代码。您的编辑可能可以为您做这件事;到处搜索如何。这样很难跟得上。

关于c - C 新手,类似二十一点的程序的错误消息问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36439789/

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