所以我在课堂上写了一个类似于二十一点的程序,我收到了以下错误:
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);
}
答案 0 :(得分:0)
有些突出的事情:
1)阅读有关如何使用printf
的内容 - 如果你是C的新手,它会有很大的不同。你传递一个字符串作为第一个参数,一个args列表作为其余参数,你代表字符串中的args使用%后跟类型标识符。例如:%d
表示int,所以:
printf("You roll 3 dice: %d %d %d", playerdie1, playerdie2, playerdie3);
2)C没有嵌套函数;您希望将int initial3roll()
的定义移到main
之外。它应该与您对main
的定义类似。
3)C没有元组,所以这不起作用:
(playerdie1,playerdie2,playerdie3) = int initial3roll();
除非您想使用指针或传入参考参数,否则您必须从int
返回单个initial3roll
。您可能想要考虑仅运行此功能3次,每次运行一次,以简化操作。或者只是完全删除它并单独使用rand
。
4)每个case
在数字后需要一个冒号,并且可能应该在它自己的行上(为了清楚起见)。 default
案例是正确的。
5)让自己更轻松,并正确缩进代码。你的编辑可能会为你做这件事;四处搜寻。像这样很难遵循。