gpt4 book ai didi

c - 用户想玩多少次游戏 c 语言

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

我正在学习如何编写游戏,我已获得以下游戏规则。

  • 规则 1:玩家必须定义停止回合的最小数字,该数字必须大于 10
  • 规则 2:本轮所有得分均计入总分
  • 规则 3:如果骰子落在 1,则该轮的所有分数均被没收
  • 规则 4:第一个获得 101 分的玩家获胜!

我试图在用户输入有多少玩家后获取要输入的游戏数量。

我的问题是每次我输入玩家姓名时都会询问用户,具体取决于我输入的玩家数量

示例

How many Players: 3

How many games: 15

Enter Players name: John

How many games: 15 (I input this number again) I dont need this line here

Enter Players name: Mary

How many games: 15 (I input this number again) I dont need this line here

Enter Players name: Barry

How many games: 15 (I input this number again) I dont need this line here

这是我解决问题的代码:

//user inputs value of player_num, here, as you have now//

printf_s("Please type in the number of players: ");
scanf_s("%d", &player_num, sizeof(int));
for (i = 0; i < player_num; i++) {
//user inputs value of num_game, here, as you have now//
printf_s("Please type in the number of games: ");
scanf_s("%d", &num_games, sizeof(int));
num_games = (j = 1, num_games);

printf_s("Enter the player's first name: ");
scanf_s("%s", names[i], 25);
getchar();
}
printf_s("\n");
for (i = 0; i < player_num; i++)
printf_s("\n%s", names[i]);

最佳答案

之所以一直要求你“输入游戏次数”,是因为你把printf和scanf语句放在了循环中。

for (i = 0; i < player_num; i++) {
printf_s("Please type in the number of games: ");//this
scanf_s("%d", &num_games, sizeof(int));//this
num_games = (j = 1, num_games); //including this

printf_s("Enter the player's first name: ");// also this
scanf_s("%s", names[i], 25); // and this
getchar();
}

因此,每次执行循环时,它都会要求您输入值。

相反,如果您希望它询问您一次,并且您希望语句只执行一次,则必须将其放在循环之外。

如果您愿意,您可以这样做:

get the number of games to be inputted in after the user enters how many players there are.

printf_s("Please type in the number of players: ");
scanf_s("%d", &player_num, sizeof(int));
printf_s("Please type in the number of games: ");
scanf_s("%d", &num_games, sizeof(int));
num_games = (j = 1, num_games);

它是

asking the user every time you enter the players names depending on how many players you have inputted because you've entered those lines "inside the loop";

关于c - 用户想玩多少次游戏 c 语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44383315/

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