2016-10-26 23 views
0

我的問題是,如何在一個while循環中保存一個int值,我的代碼完全是關於賭博的,你從1,000開始,並且希望賺到最多的現金,但是當我再次滾動我的現金時,達到我設定的原始價值。如何在while循環中保存int值?

我的代碼是這樣的(注意我新,所以不要在它是多麼糟糕笑)

#include <cmath> 
#include <stdio.h> 
#include <iostream> 
#include <ctime> 
#include <cstdlib> 
using namespace std; 


int main() 
{ 
    char again = 'Y'; 
    int test; 
    int yes; 
    int CashW; 
    CashW = 1000; 
    int CashL; 
    CashL = 1000; 
    int yLose; 
    yLose = 500; 
    int xCash; 
    xCash = 1000; 
    int xRan; 
    srand(time(0)); 
    xRan = rand() % 100 + 1; 
    cout << " Welcome to the Gambling Game!" << endl; 
     cout << " If the number is above 50 I win!" << endl; 
     cout << " If the number is below 50 you lose!" << endl; 
     while (again == 'y' || again == 'Y') 
     { 
     cout << " The Number I Choose Is: " << xRan << endl; 


     CashL = xCash - xCash - xCash; 
     CashW = xCash + xCash; 


     if (xRan < 50) { 
      cout << " You win, rats!" << endl; 
      cout << " The cash you started with was: " << xCash << endl; 
      cout << " The cash you have now is: " << CashW << endl; 
      cout << " Type 1 to play again, type 2 to close the game." << endl; 
      cin >> yes; 

     } 

     if (xRan > 50) { 
      cout << " I win, you lose!" << endl; 
      cout << " The cash you started with was: " << xCash << endl; 
      cout << " The cash you have now is: " << CashL << endl; 
      cout << " Type 1 to play again, type 2 to close the game." << endl; 
      cin >> yes; 

     } 

     if (yes == 1) { 
      cout << " Cool, a gambling man! Time to make some cash" << endl; 
     } 

    } 


} 
+1

您應該只使用一個變量來存儲當前的現金玩家。在if語句中增加或減少獲勝或失敗。您必須在while循環內移動隨機數生成,以便在每一回閤中獲得不同的結果。 –

回答

1

在你的代碼目前顯示無論是CashWCashL取決於gampbling結果。

不幸的是,你只打印出結果,並從不將它存儲到xCash。所以在下一次迭代中,您將再次使用相同的xCash值!

您可以通過在顯示結果的行的正下方添加xCash = CashW;xCash = CashL;來輕鬆解決此問題。

+0

這可能應該是'xCash + = CashW'和'xCash - = CashL'。 –

+0

@RemyLebeau其實我猶豫了。 OP顯示的文本是:*「您現在擁有的現金是......」*。所以我認爲這確實是總的而不是增量的勝利。但感謝評論意見:它會幫助OP進行調整,以防我誤解。 – Christophe

1

你永遠不會更新xCash與每贏得/損失的金額。您不會在每次循環迭代中生成一個新的隨機數。而且你陷入無限循環,因爲你永遠不會更新循環變量again

嘗試一些更喜歡這個:

#include <cmath> 
#include <stdio.h> 
#include <iostream> 
#include <ctime> 
#include <cstdlib> 

using namespace std; 

int main() 
{ 
    const int CashW = 1000; 
    const int CashL = 1000; 

    int xCash = 1000; 
    int xRan; 
    char answer; 

    srand(time(0)); 

    cout << " Welcome to the Gambling Game!" << endl; 
    cout << " If the number is above 50 I win!" << endl; 
    cout << " If the number is below 50 you win!" << endl; 

    do 
    { 
     xRan = rand() % 100 + 1; 
     cout << " The Number I Choose Is: " << xRan << endl; 

     if (xRan < 50) { 
      cout << " You win, rats!" << endl; 
      cout << " The cash you started with was: " << xCash << endl; 
      xCash += CashW; 
      cout << " The cash you have now is: " << xCash << endl; 
     } 

     else if (xRan > 50) { 
      cout << " I win, you lose!" << endl; 
      cout << " The cash you started with was: " << xCash << endl; 
      xCash -= CashL; 
      cout << " The cash you have now is: " << xCash << endl; 
     } 

     else { 
      cout << " dang, a draw!" << endl; 
     } 

     cout << " play again? " << endl; 
     cin >> answer; 

     if ((answer != 'y') && (answer != 'Y')) { 
      cout << " All done? Come back again another time!" << endl; 
      break; 
     } 

     cout << " Cool, a gambling man! Time to make some cash" << endl; 
    } 
    while (true); 

    return 0; 
}