我收到变量可能未初始化的错误。我使用普通的字符串数据类型和 if..else 语句进行决策。这是一个关于向用户发放 5 张随机卡片的程序。我认为,我得到的错误出现在 if else 语句的第一个语句中变量赋值方法是正确的,甚至在其他程序中尝试过,并且在这些程序中有效。有人可以找到其中的错误吗?
import java.util.Random;
public class RandomCard
{
public static void main(String[] args)
{
int cardNumber;
int cardSign;
String sign;
Random card = new Random();
for(int counter = 0; counter < 5; counter++)
{
cardSign = card.nextInt(4);
if(cardSign == 0)
{
sign = "Spades";
}
else if(cardSign == 1)
{
sign = "Hearts";
}
else if(cardSign == 2)
{
sign = "Clubs";
}
else if(cardSign == 3)
{
sign = "Diamond";
}
cardNumber = 1 + card.nextInt(13);
if(cardNumber>1 && cardNumber<11)
{
System.out.print(sign + " ");
System.out.println(cardNumber);
}
else if(cardNumber == 11)
{
System.out.print(sign + " ");
System.out.println("Jack");
}
else if(cardNumber == 11)
{
System.out.print(sign + " ");
System.out.println("Jack");
}
else if(cardNumber == 12)
{
System.out.print(sign + " ");
System.out.println("Queen");
}
else if(cardNumber == 13)
{
System.out.print(sign + " ");
System.out.println("King");
}
else if(cardNumber == 1)
{
System.out.print(sign + " ");
System.out.println("ACE");
}
}
}
}