![]() |
C程式的題目~求救
Write a program to simulate a 6 star lottery game. The rule is as follows:
1.A lottery ticket cost NT $50. 2.The player can pick six numbers from 1 to 42. 3.There is no duplicate lottery number. 4.The prize is shown as follows: Reward Item Rule Prize 6 Stars Price 6 matched numbers 500,000 5 Stars Price 5 matched numbers 10,000 4 Stars Price 4 matched numbers 1,000 3 Stars Price 3 matched numbers 200 1. Display a welcome message to the game. A player enters her or his name. 2. A player can select the number of tickets the player wants to buy and decide if the computer selects the numbers or the player selects the numbers. 3. A player enters the numbers or the computer selects the numbers based on the player's decision. 4. Print the receipt. 5.The player can choose to buy more tickets or not. 6. Show the lottery result. 7. Calculate how much the player spends and wins. 8.The player can choose to continue a new game. 編譯結果如下 ================================================= Welcome to the Happy Lottery Game! Your name: Rita Rita, welcome to the game! ================================================= Enter the number of tickets you want to buy: 2 Computer or Self select (C/S)? S Select numbers: 17 2 6 22 32 42 Select numbers: 10 6 12 7 28 36 ------------------------------------------------- Rita's Receipt: Total pay: 100 Thanks for your playing Happy Lottery! ================================================= Play more (Y/N)? Y ================================================= Enter the number of tickets you want to buy: 1 Computer or Self select (C/P)? P Select numbers: 33 7 18 26 45 36 ------------------------------------------------- Rita's Receipt: Total pay: 50 Thanks for your playing Happy Lottery.. ================================================= Play more (Y/N)? N The lottery result is: 3 7 12 28 38 41 Rita! Congratulations! You won 3 stars. You spent NT $150 and won NT $200. ================================================= Quit the game (Y/N)? N ================================================= Welcome to the lottery game! Your name: Mary Rita, welcome to the game! ================================================= ... ... ================================================= Quit the game (Y/N)? Y ================================================= Thanks for playing! 我不會寫:on_79::on_51:投降:on_22: welcome (char name[]) { printf("===========================================\n"); printf("Welcome to the Happy Lottery Game!\nYour name: "); printf("Your name: "); scanf("%s", name); printf("%s, welcome to the game!\n", name); printf("===========================================\n"); } main() { char name[80]; // string = an array of chars int no_ticket; int lotto[10][3]; // an array for the selected lottery numbers char more; do { welcome(name); printf("Enter the number of tickets you want to buy: "); scanf("%d", &no_ticket); printf("Select 3 numbers: "); scanf("%d%d%d", &lotto[0][0], &lotto[0][1], &lotto[0][2]); printf("You select %d %d %d.\n", lotto[0][0], lotto[0][1], lotto[0][2]); scanf("%c", &more); // read the newline printf("=================================================\n"); printf("Play more (Y/N)? "); scanf("%c", &more); } while (more == 'Y' || more == 'y'); printf("=================================================\n"); printf("Thanks for playing!\n"); } 只寫到以上的其他就不會寫了 |
我是用Virtual C 寫的...
我是用Virtual C 寫的...
換平台有些寫法可能要自己改一下哦! 看不懂再問吧!! 有點複雜的說加上貼上來後格式會全跑掉... ><" 我在想想看可不可以弄成Class或方法嗎.... 有想到再給你看看摟! 其他人有寫出來可以借我看看嗎? 我想多學幾種方法... 感謝喔!! 可執行檔放在 http://www.filedropper.com/test2_1 你可以先試試看再去看程式碼!!! 比較好懂! 我是先用固定的開獎號碼做喔!! 要改自動產生開獎號碼把註解地方(/* */)拿掉就可以了! #include "stdafx.h" #include "stdlib.h" #include "stdio.h" #include "time.h" using namespace System; int main(array<System::String ^> ^args) { int i, j, k, tickets, pay=0, total, totalpay, select[6]={3,7,12,28,38,41}, **Computerselect=new int *[tickets], stars, Maxstars; //tickets是購票數量,pay每次購買票數金額,totalpay總共要付多少錢,total中獎金額,select開獎號碼,Computerselect玩家購買票號(用二維動態陣列存),stars每組中幾個號碼,Maxstars最多中幾個號碼 char name[128], choice; //name玩家名字,choice選擇用(有重覆使用!) do{ Maxstars=0; //因為可能有多位玩家! 所以每次開始時要清空前筆資料! stars=0; totalpay=0; total=0; printf("=================================================\n"); printf("Welcome to the Happy Lottery Game!\nYour name:"); scanf("%s",name); printf("%s, welcome to the game!\n",name); srand((unsigned)time(NULL)); //產生亂數用! 網路上都查的到! /* for(i=0; i<6; i++) //自動選則開獎號碼(隨機產生的開獎號碼) { d: select[i]=rand()%37; for(j=0; j<i; j++) //檢查選號是否重覆 { if(select[i]==select[j] || select[i]==0) //檢查選號是否重覆或產生0的亂數 goto d; } } */ do{ //進入遊戲 printf("=================================================\nEnter the number of tickets you want to buy:"); scanf("%d",&tickets); for(i=0; i<tickets; i++) { Computerselect[i] = new int[6]; } c: printf("Computer or Self select (C/S)?"); scanf(" %c",&choice); //%c前一定要空一格才會等待輸入!! 為什麼~因為經驗!!! 不空格會直接往下直行喔!!! switch(choice) { case 'C': case 'c': stars=0; for(i=0; i<tickets; i++) //機器自動選號 { printf("Select numbers:"); for(j=0; j<6; j++) //機器自動選號 { a: Computerselect[i][j]=rand()%37; for(k=0; k<j; k++) //檢查選號是否重覆 { if(Computerselect[i][j]==Computerselect[i][k] || Computerselect[i][j]==0) //檢查選號是否重覆或產生0的亂數 goto a; //a為標籤! 會跳到 a: 的地方開始執行 } printf(" %d",Computerselect[i][j]); } printf("\n"); } for(i=0; i<tickets; i++) //判斷中幾顆星 { for(j=0; j<6; j++) { for(k=0; k<6; k++) { if(Computerselect[i][j]==select[k]) //判斷數字是否重複(中獎) { stars++; } } } } if(Maxstars<stars) //將最多的中獎數量存起來 Maxstars=stars; printf("-------------------------------------------------\n"); break; case 'S': case 's': stars=0; for(i=0; i<tickets; i++) //自行選號 { printf("Select numbers:"); b: scanf("%d %d %d %d %d %d",&Computerselect[i][0], &Computerselect[i][1], &Computerselect[i][2], &Computerselect[i][3], &Computerselect[i][4], &Computerselect[i][5]); for(j=0; j<6; j++) { if(Computerselect[i][j]<1 || Computerselect[i][j]>42) //判斷數字是否超出範圍 { printf("數字超出範圍! 請重新輸入!\n"); goto b; } for(k=0; k<j; k++) //檢查選號是否重覆 { if(Computerselect[i][j]==Computerselect[i][k]) //判斷數字是否重覆 { printf("數字重覆! 請重新輸入!\n"); goto b; } } } } for(i=0; i<tickets; i++) //判斷中幾顆星 { for(j=0; j<6; j++) { for(k=0; k<6; k++) { if(Computerselect[i][j]==select[k]) //判斷數字是否重複(中獎) { stars++; } } } } if(Maxstars<stars) //將最多的中獎數量存起來 Maxstars=stars; printf("-------------------------------------------------\n"); break; default: printf("代碼錯誤!! 請重新輸入!\n"); goto c; } //結束switch pay=tickets*50; totalpay+=pay; printf("%s's Receipt:\nTotal pay: %d\nThanks for your playing Happy Lottery!\n",name, pay); printf("=================================================\nPlay more (Y/N)?"); scanf(" %c",&choice); }while(choice=='Y' || choice=='y'); printf("The lottery result is:\n"); for(i=0; i<6; i++) //印出開獎號碼 { printf(" %d",select[i]); } printf("\n"); if(Maxstars>2) //判斷中獎金額 total=200; else if(Maxstars>2) total=200; else if(Maxstars>3) total=1000; else if(Maxstars>4) total=10000; else if(Maxstars>5) total=500,000; printf("%s! Congratulations! You won %d stars.\nYou spent NT $%d and won NT $%d.\n=================================================\nQuit the game (Y/N)?",name, Maxstars, totalpay, total); scanf(" %c",&choice); }while(choice=='N' || choice=='n'); printf("Thanks for playing!\n"); system("pause"); return 0; } |
所有時間均為台北時間。現在的時間是 02:54 PM。 |
Powered by vBulletin® 版本 3.6.8
版權所有 ©2000 - 2025, Jelsoft Enterprises Ltd.
『服務條款』
* 有問題不知道該怎麼解決嗎?請聯絡本站的系統管理員 *