gpt4 book ai didi

c - C的段错误

转载 作者:行者123 更新时间:2023-12-01 16:19:01 25 4
gpt4 key购买 nike

我的程序是一个简单的“剪刀石头布蜥蜴 Spock”​​程序,我想添加一个 2 位玩家或 1 位玩家对战计算机的选项。这就是我对选择部分进行编码的方式:

printf("1.One player OR 2.Two players?\n");
scanf("%d", playerNum);
//input section
printf("1.rock\n2.paper\n3.scissor\n4.spock\n5.lizard\n");
printf("player A enter a choice ");
scanf ("%d", &choiceA);
if (playerNum == 2)
{
printf("1.rock\n2.paper\n3.scissor\n4.spock\n5.lizard\n");
printf("player B enter a choice ");
scanf ("%d", &choiceB);
}
else if (playerNum == 1)
{
choiceB = 1+rand()%5;
printf("The computer has picked %d", choiceB);
}

当我运行该程序时,在我为 playerNum 输入一个值后立即出现段错误
上面使用的所有变量都声明为整数。

最佳答案

你的 scanf 需要一个 int * 类型的参数,而你正在传递它 int 类型的参数。
您在 scanf 的参数中缺少 &

scanf("%d", playerNum);
^
|
& is missing.

将此更改为

scanf("%d", &playerNum);

关于c - C的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19328314/

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