我有一个班级的例子。它创建一张新卡并将其放入数组中。如果卡数超过54,我想控制卡数并离开流程:
public class Card {
private final String rank;
private final String suit;
private String[][] cards;
private static int NoC = 0;
public Card(String suit, String rank) {
if (NoC >= 54) {
System.out.println("You cannot create any more cards");
// and this "return" doesn't work correctly. NetBeans says "variable rank might not have been initialized"
return;
}
this.suit = suit;
this.rank = rank;
cards = new String[54][2];
cards[NoC][0] = this.suit;
cards[NoC][1] = this.rank;
NoC = NoC + 1;
}
程序编译无误。并且按我的预期工作:如果正在创建第 55 张卡片,它会转到IF
子句并打印通知“您不能再创建更多卡片”,但它也会引发警告“无法编译的源代码 - 变量等级可能尚未初始化”。
如何让它正确运行?我是否必须使用 smth else 而不是return
命令?