我有下面的代码,并且想知道为什么当我运行该程序时返回null而不是实际值?任何帮助都会受到干扰。为什么返回空值而不是值?
import java.util.Random;
public class TestCard {
public static String[] possCards = new String[]{"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
public static String[] possSuits = new String[]{"C", "S", "H", "D"};
public static Random rand = new Random();
static String value;
public static void main(String[] args) {
System.out.println(getcard());
}
public static void card() {
String card = possCards[rand.nextInt(possCards.length)];
String suit = possSuits[rand.nextInt(possSuits.length)];
value = card + suit;
}
public static String getcard(){
return value;
}
}
你永远不叫'卡()'.. –
String的缺省值为空。你从来没有初始化字符串的任何值 – newuser
我认为调试的第一步是学习在每个方法中添加System.out.println以逐块跟踪方法 – Jianhong