我正在编写一个程序,该程序将打印一张盒装扑克牌,该牌根据输入显示不同的数字和花色。方法 printCard(Stringsuit, int kind) 将采用字符串“heart”、“club”、“diamond”或“spade”。任何其他输入都会打印一张没有花色的牌。kind 将输入视为 int 1 -13,任何其他输入都会打印一张没有数字的卡片,我使用了 switch case 来指定套装,并且代码运行良好,但现在当我使用 switch case 来指定类型时,我会抛出错误。
class PrintCard {
public static void printCard(String suit, int kind) {
System.out.println("╔═════════╗");
switch (kind) {
default:
System.out.println("║ ║");
case 1:
System.out.println("║ A ║");
case 2:
System.out.println("║ 2 ║");
case 3:
System.out.println("║ 3 ║");
case 4:
System.out.println("║ 4 ║");
case 5:
System.out.println("║ 5 ║");
case 6:
System.out.println("║ 6 ║");
case 7:
System.out.println("║ 7 ║");
case 8:
System.out.println("║ 8 ║");
case 9:
System.out.println("║ 9 ║");
case 10:
System.out.println("║ 10 ║");
case 11:
System.out.println("║ J ║");
case 12:
System.out.println("║ Q ║");
case 13:
System.out.println("║ K ║");
}
System.out.println("║ ║");
System.out.println("║ ║");
switch (suit) {
default:
System.out.println("║ ║");
break;
case "club":
System.out.println("║ ♣ ║");
break;
case "diamond":
System.out.println("║ ♦ ║");
break;
case "heart":
System.out.println("║ ♥ ║");
break;
case "spade":
System.out.println("║ ♠ ║");
break;
}
System.out.println("║ ║");
System.out.println("║ ║");
switch (kind) {
default:
System.out.println("║ ║");
case 1:
System.out.println("║ A ║");
case 2:
System.out.println("║ 2 ║");
case 3:
System.out.println("║ 3 ║");
case 4:
System.out.println("║ 4 ║");
case 5:
System.out.println("║ 5 ║");
case 6:
System.out.println("║ 6 ║");
case 7:
System.out.println("║ 7 ║");
case 8:
System.out.println("║ 8 ║");
case 9:
System.out.println("║ 9 ║");
case 10:
System.out.println("║ 10 ║");
case 11:
System.out.println("║ J ║");
case 12:
System.out.println("║ Q ║");
case 13:
System.out.println("║ K ║");
}
System.out.println("╚═════════╝");
}
}
我期待打印一个 printCard("heart", 5) 的示例
╔═════════╗
║.5.......║
║.........║
║.........║
║....♥....║
║.........║
║.........║
║.......5.║
╚═════════╝
但是我得到了一个错误:
> Main.java:11: error: incompatible types: int cannot be converted to String
> PrintCard.printCard(a0, a1);
> ^
> Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
> 1 error
我不明白,因为在我的 switch case 中,我使用整数来表示我的 case,当我在顶部声明参数时,我将其指定为 int。我哪里错了?
打开字符串需要 Java 7 或更高版本 - 确保您有 7+ JDK 并且源/目标代码级别也启用为 7+