簡體   English   中英

在設定金額輪次后,我如何告訴我的應用程序“停止運行”?

[英]How do i tell my app “stop running” after set amount rounds?

所以我正在制作一個基於轉彎的骰子游戲,模仿這個名為“地下chinchiro”的游戲,該游戲取自動畫片“Kaiju”。 我需要為我的程序設置一個限制,以便它只運行一定數量的輪次,我只是編碼的初學者,所以很遺憾你在我的代碼中看到的任何異常。

#include <iostream>
#include <string>
#include <time.h>
using namespace std;

void roll_3_dice(int &dice1, int &dice2, int &dice3) 
{
    dice1 = rand() % 6 + 1;
    dice2 = rand() % 6 + 1;
    dice3 = rand() % 6 + 1;
    return;
}


int main()
{
int cash = 90000;
int wager; 
int r;


//dealer's die
int dealer1;
int dealer2;
int dealer3;

// your die
int mdice1;
int mdice2;
int mdice3;


for (int i = 0; i < 10; i++)
{
cout << "Wager up boy!"<< endl;
cin >> wager;
while (wager < 100 || wager > 90000)
{
cout << "Minimum wager is 100; Maximum wager is 90000 ";
cin >> wager;
}

cout << "You wagered: " << wager << endl;
cout << "You have " << cash - wager << " remaining" << endl;
cash = cash - wager;

cout << endl;
cout << "Dealer will now roll the dice" << endl;

roll_3_dice(dealer1, dealer2, dealer3);


cout << "Dealer rolled the following: " << endl;
cout << dealer1 << "-" << dealer2 << "-" << dealer3 << endl;

cout << "It's your turn to roll the dice." << endl;
cout << endl; 
cout << "Press any key to roll the dice" << endl;
cin >> r;

roll_3_dice(mdice1, mdice2, mdice3);

cout << "You rolled the following: " << endl;
cout << mdice1 << "-" << mdice2 << "-" << mdice3 << endl;


system ("pause");
 }
}

看看for循環。 For循環允許您運行代碼進行一定數量的迭代。

例如,迭代一些代碼7次。

int number_of_iterations = 7;
for(int i = 0; i < number_of_iterations; i++) {
    // Your code that you would like to iterate over goes here.
 }

編輯:正如OP所指定的那樣(在下面的評論中),問題似乎是程序沒有停止通過for循環的每次迭代接收來自用戶的輸入。

這可能有很多原因。 我最好的猜測是stdin緩沖區不清楚,並且std::cin繼續從緩沖區讀入。 這可以通過在讀入輸入之前調用cin.clear()來解決。

是時候學習如何使用常數...

定義一個做

const int max_round = 5;

並且做了一段while這么長的回合是<=max_round

你的問題很不清楚。 編輯代碼,找到發生問題的部分並僅粘貼該部分。

喜歡:

while(cin>>wager)
{
   if(condition fails) 
   { 
       break; 
   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM