如何从编译器中获取随机数

时间:2018-01-16 11:59:11

标签: java random

我正在创建一个简单的纸牌游戏,其中使用随机数创建卡片。我想使用随机数作为得分int。有没有办法添加用于生成特定卡的随机数?例如,级别的随机数:5。 Totalscore = totalscore + RandomNumber。

卡类

public class clsCard {
    //Main variables of my program
    private int value;
    private int level, object;
    private final int MaxFaceValue= 10;
    private static String[] objects =  {"Joker","hearts","spades","diamond","clubs"};
    private static String[] levels = {"Joker","Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"};

    public clsCard()  {
        this.level= (int)(Math.random()*levels.length);
        this.object= (int)(Math.random()*objects.length);
    }

    public @Override String toString() { 
        if(levels[level].equals("Joker") || objects[object].equals("Joker"))
            return "Joker";
        else
            return levels[level] + " of " + objects[object];
    }

    public int getLevel() {
        return level;
    }

    public int getObject() {
        return object;
    }
}

主程序

clsCard cardFace = new clsCard();           
System.out.println(cardFace);

0 个答案:

没有答案