2017-06-06 22 views
0

我正在学习如何编写一个游戏,我已经给了游戏的以下规则。用户想玩多少次c语言

  • 规则1:玩家必须定义一个最小数目以停止此必须转弯大于10
  • 规则2:从圆形所有点被添加到总
  • 规则3:如果骰子第一名得分为1,那一轮的所有得分都被没收
  • 规则4:第一名获得101分的玩家获胜!

我想在用户输入有多少玩家后输入游戏的数量。

我的问题是,它要求用户每次我取决于我有多少玩家输入

多少玩家进入球员的名字:3

多少游戏:15

输入玩家姓名:约翰

多少游戏:15(我再次输入这个号码)我不需要这条线在这里

输入球员姓名:玛丽

多少游戏:15(我再次输入这个号码)我不需要在这里这条线

输入球员姓名:巴里

多少游戏:15(我再次输入这个号码),我不需要在这里这条线

这是我的问题代码:

//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]); 
+1

构建您的代码,以便它符合您的要求。 – ThingyWotsit

+0

您可以改进您的[mcve],但它是全球性的一个很好的问题。 – Stargateur

+1

https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – TessellatingHeckler

回答

0

你的循环有2 scanf,而你只需要一个。将这个

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

在循环的前面,而不是在它里面。

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: "); // no need for this in the loop 
    scanf_s("%d", &num_games, sizeof(int));   // no need for this in the loop 
    num_games = (j = 1, num_games);     // no need for this in the loop 


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

我把这个陈述摆脱了循环。现在它崩溃了,但我决定我需要改变那个特定循环的名字,并利用这个bug并将它变成游戏中的一个条件,它要求用户停止轮到它的最小数量。它实际上并没有真正解决我提出问题的顺序如下 –

+0

1.问有多少玩家(必须超过1个玩家) 2.有多少游戏(只问一次) 3.用户名(询问与输入号码1相同的次数) 4.最小号码停止(询问输入号码1的次数必须大于10) –

+1

@ElijahCeeney您没有显示MCVE。我只能指出你显示的代码中有什么问题...... – CIsForCookies

0

为什么它一直为“输入游戏的数量”再次问你又一次的原因是因为你已经把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(); 
} 

由于每次执行循环时都会要求输入值。

相反,如果你想让它问你一次,你必须把它放在循环之外,如果你想要只执行一次语句。用户进入后 许多球员有怎样的

得到需要输入的游戏数量:

,如果你愿意,你可以做到这一点。

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); 

,这是

要求用户方便地根据您如何 很多玩家输入 因为你已经进入了“内循环”的行中输入玩家名字,每次;