基本上,我在計算機生成隨機卡的地方製作了這個程序。我做了一個叫'rndnumber'的類,這個類生成隨機數。然後我做了另一個叫做'rndsuits'的課程,這會產生隨機訴訟。 問題是,當我去我的主要類,並執行代碼,我得到null,而不是數字和適合。有人知道爲什麼我的輸出基本上是: 這是你的隨機卡片:null爲null。當從另一個類中檢索int時變爲空
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;
}
}
}
你真的應該使用枚舉數據這樣的孩子。 – Dariusz