1

我在介绍课上一直在努力,我几乎完成了我的最后一个项目,基诺。它是一个投注游戏,根据您与庄家匹配的数字来奖励金钱。我在投注方面存在问题,他们从 100 美元开始,并被要求支付一定数量的钱。我不知道哪种方法仍然可以工作,因为我的方法不是 void,所以我不能返回多个数据值。

我的第二个问题,也许是更重要的一个,是它们必须是唯一的数字。为此,我需要每次都搜索数字数组以查看它们是否匹配,或者使用布尔数组来跟踪数字。我不知道我会如何做第二个,但我很清楚我会如何处理第一个。问题是我已经在使用 do while 了,我不确定如何添加带有嵌套 for 循环的 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;
    }

}
4

1 回答 1

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 个数字来说,这似乎很多。

于 2013-04-20T13:40:59.900 回答