2013-04-20 90 views
1

我在我的介紹類在努力前進,我已經差不多完成了我的最後一個項目,基諾。它是一種根據您與經銷商匹配的數字獎勵金錢的博彩遊戲。我在投注方面有問題,他們以100美元開始,並被要求支付一定數額的金錢。我不知道哪種方法可以繼續工作,因爲我的方法不是空洞的,所以我不能返回多個數據值。投注和搜索的唯一性

我的第二個問題,也許更重要的是,他們必須是唯一的編號。爲此,我需要每次搜索數字數組以查看它們是否匹配,或者使用布爾數組來跟蹤數字。我不知道我會怎麼做第二,但我有一個很好的想法,我會做第一次。該問題是使用DO,即時通訊,而已經,林不知道我會如何與嵌套添加for循環中循環這裏是我的代碼,對不起,如果它的凌亂,我知道我的老師不喜歡我的大括號:

package Keno; 

import cs1.Keyboard; 

public class Keno { 
    public static void main(String[]args){ 
     int userArr[]=user(); 
     int compArr[]=computer(); 
     int howMany=matchNums(compArr,userArr); 
     int moneyGained=betting(howMany); 

     System.out.println("You matched "+howMany+" numbers"); 
     System.out.println("You have gained "+moneyGained+" dollars!"); 

    } 

    public static int[] computer(){ 
     int []compChoice=new int[20]; 
     for(int x=0;x<compChoice.length;x++){ 
      compChoice[x]=(int)(Math.random()*81); 
     } 
     return compChoice; 
    } 
    public static int[] user(){ 
     int choice[]=new int[7]; 
     System.out.println("Welcome to Keno!"); 
     System.out.println("Choose 7 unique numbers ranging from 1-80"); 
     System.out.println("*************************************************"); 
     //assigns numbers to choice array 
     for(int x=0;x<choice.length;x++){ 
      do{ 
       int temp=x+1; 
       System.out.println("number "+temp+": "); 
       choice[x]=Keyboard.readInt(); 
      }while(choice[x]<0||choice[x]>80); 

     } 
     System.out.println("Thanks!"); 
     System.out.println("*************************************************"); 
     return choice; 

    } 
    public static int matchNums(int arr1[], int arr2[]){ 
     int count=0; 
     //checks each array slot individually to see if they match 
     for(int x=0;x<arr1.length;x++){ 
      for(int y=0;y<arr2.length;y++){ 
       if(arr1[x]==arr2[y]){ 
        count++; 
       } 
      } 
     } 
     return count; 
    } 
    public static int betting(int matches){ 
     int moneyGained=0; 
     if(matches==7){ 
      moneyGained=12000; 
     }else if(matches==6){ 
      moneyGained=200; 
     }else if(matches==5){ 
      moneyGained=20; 
     }else if(moneyGained==4){ 
      moneyGained=1; 
     } 
     return moneyGained; 
    } 

} 
+0

對於第一個問題,何談創造,將保持當前的資金跟蹤變量並編寫返回金錢的用戶已決定工資額的方法? (然後可以用來更新您的初始變量) – 2013-04-20 13:40:02

+0

或者使用ArrayList和檢查。載有(數字) – arynaq 2013-04-20 14:00:51

回答

0

添加投注/金錢觀將是添加代表多少錢的玩家(100起)的整數最簡單的方法。你必須詢問玩家他們想要下注多少,然後相應地調整他們的錢。

public static void main(String[] args) { 
    int playerMoney = 100; 
    int wagered = getWager(); // dont forget it has to be 0 < wagered <= 100 
    // adjust players money according to the wager, and how much they won 

爲確保唯一性,您的任何一個想法都可以發揮作用。我喜歡只是檢查數組中已存在的數字,但大小爲80的布爾數組也可以。儘管只有7個數字,但似乎很多。

+0

謝謝大家,我一定會做到這一點。我不知道我會如何添加該方法,並使其工作沒有衝突,但現在我很好。 – 2013-04-20 16:20:34