返回列表 發帖

賽馬程式 (五)

本帖最後由 陳品肇 於 2019-1-5 16:05 編輯

1. 完成主選單的主要架構 (運用if...else if...else判斷式)
2. 新增 (2)下注 功能
  1. #include<iostream>
  2. [code]#include<iostream>
  3. #include<cstdlib>
  4. using namespace std;
  5. int main()
  6. {
  7.    
  8.     int n=1,money=0;
  9.     re1:
  10.     int a=0, b=0, c=0, d=0, r,option,buying,bet;
  11.     system("cls");
  12.     cout<<"「好事成雙」賽馬場 第 "<<n<<" 局"<<endl;
  13.     cout<<"------------------------------------------------------------------------| 終點"<<endl;
  14.     cout<<"◆"<<endl;
  15.     cout<<"★"<<endl;
  16.     cout<<"▲"<<endl;
  17.     cout<<"●"<<endl<<endl;
  18.     cout<<"可用餘額: "<< money<<"元"<<endl<<endl;
  19.     cout<<"(1)買入 (2)下注 (3)離開 請選擇:";
  20.     cin>>option;
  21.     if(option==1)  //選擇買入多少籌碼
  22.     {
  23.       
  24.        cout<<"買入:";
  25.        cin>>buying;
  26.        money = money + buying; // 原始的餘額加上買入的籌碼
  27.        goto re1;
  28.     }else if(option==2)
  29.     {
  30.        if(money==0)   //一開始就沒有餘額
  31.        {
  32.            cout<<"可用餘額不足!請先買入!"<<endl;
  33.            system("pause");
  34.            goto re1;
  35.        }
  36.       
  37.         cout<<"下注:";
  38.         cin>> bet;
  39.         if(bet>money)   //餘額< 下注金額
  40.         {
  41.            cout<<"可用餘額不足!請先買入!"<<endl;
  42.            system("pause");
  43.            goto re1;
  44.         }else if(bet < money)
  45.         {
  46.              cout<<"(1)◆(2)★(3)▲(4)● 請選擇: "<<endl;
  47.              cin>>option;
  48.             cout<<"比賽即將開始..."<<endl<<endl;
  49.             system("pause");   
  50.         }
  51.         
  52.     }else if(option==3)
  53.     {
  54.           goto end;
  55.     }else
  56.     {
  57.          cout<<"輸入錯誤!!"<<endl;
  58.           system("pause");
  59.          goto re1;
  60.     }
  61.    
  62.    
  63.     system("cls");
  64.     srand(time(NULL));
  65.     while(a!=75 && b!=75 && c!=75 && d!=75)
  66.     {
  67.         r=rand()%4;   //產生0~3之隨機亂數
  68.         if(r==0)
  69.             a++;
  70.         else if(r==1)
  71.             b++;
  72.         else if(r==2)
  73.             c++;
  74.         else
  75.             d++;
  76.         cout<<"比賽進行中"<<endl;
  77.         cout<<"------------------------------------------------------------------------| 終點"<<endl;
  78.         for(int i=1; i<=a; i++)
  79.             cout<<" ";
  80.         cout<<"◆"<<endl;
  81.         
  82.         for(int i=1; i<=b; i++)
  83.             cout<<" ";
  84.         cout<<"★"<<endl;
  85.         
  86.         for(int i=1; i<=c; i++)
  87.             cout<<" ";
  88.         cout<<"▲"<<endl;
  89.         
  90.         for(int i=1; i<=d; i++)
  91.             cout<<" ";
  92.         cout<<"●"<<endl;   
  93.         system("cls");      
  94.     }
  95.     // 比賽結束時 第一名的馬
  96.     cout<<"比賽結束! 由 ";
  97.     if(a==75)
  98.     {
  99.         cout<<"◆";
  100.     }
  101.     else if(b==75)
  102.     {
  103.         cout<<"★";
  104.     }
  105.     else if(c==75)
  106.     {
  107.         cout<<"▲";
  108.     }
  109.     else
  110.     {
  111.         cout<<"●";
  112.     }
  113.       
  114.     cout<<" 先馳得點!"<<endl;
  115.     // 印出最後4匹馬的結果
  116.     cout<<"------------------------------------------------------------------------| 終點"<<endl;
  117.     for(int i=1; i<=a; i++)
  118.         cout<<" ";
  119.     cout<<"◆"<<endl;
  120.    
  121.     for(int i=1; i<=b; i++)
  122.         cout<<" ";
  123.     cout<<"★"<<endl;
  124.    
  125.     for(int i=1; i<=c; i++)
  126.         cout<<" ";
  127.     cout<<"▲"<<endl;
  128.    
  129.     for(int i=1; i<=d; i++)
  130.         cout<<" ";
  131.     cout<<"●"<<endl;  
  132.    
  133.    
  134.    
  135.     system("pause");
  136.    
  137.     n++;
  138.     goto re1;
  139.     end:
  140.     system("pause");
  141.     return 0;   
  142. }
複製代碼

返回列表