2017-06-06 19 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); 

,這是

要求用戶方便地根據您如何 很多玩家輸入 因爲你已經進入了「內循環」的行中輸入玩家名字,每次;