基本上我做了这个程序,计算机生成随机卡。我做了一个名为'rndnumber'的类,这个类生成随机数。然后我制作了另一个名为“rndsuits”的类,这会生成随机套装。问题是当我去我的主课并执行代码时,我得到的是 null 而不是数字和西装。有谁知道为什么?我的输出基本上是:这是你的随机卡:null of null。
public class maincard {
public static void main(String[] args){
System.out.println("Here is your random card");
rndnumber h = new rndnumber();
rndsuit a = new rndsuit();
System.out.println(h.getString() + " of " + a.getStringz());
}
}
public class rndnumber {
private int rndnumber = (int) (Math.random()*13+1);
private String number;
public String getString(){
return number;
}
public void rnd(){
switch (rndnumber){
case 1:
number = "Ace";
break;
case 2:
number = "2";
break;
case 3:
number = "3";
break;
case 4:
number = "4";
break;
case 5:
number = "5";
break;
case 6:
number = "6";
break;
case 7:
number = "7";
break;
case 8:
number = "8";
break;
case 9:
number = "9";
break;
case 10:
number = "10";
break;
case 11:
number = "Jacks";
break;
case 12:
number = "Queens";
break;
case 13:
number = "Kings";
break;
}
}
}
public class rndsuit {
private int y = (int) (Math.random()*3+1);
private String rndsuit;
public String getStringz(){
return rndsuit;
}
public void suit(){
switch(y){
case 1:
rndsuit = "Spades";
break;
case 2:
rndsuit = "Cloves";
break;
case 3:
rndsuit = "Hearts";
break;
case 4:
rndsuit = "Diamonds";
break;
}
}
}