在为我的应用程序设定任何条件之前,我需要能够让 switch 语句在 while 循环下运行。据说它无法在1.7以下的任何编译器下工作。 JRE。我不知道这意味着什么,也不知道如何将变量更改为枚举类型。
package PokerApp;
import java.util.Scanner;
public class PokerApp {
public static void main(String[] args) {
int card1 = 0;
int card2 = 0;
int play;
String fc1 = "", fc2 ="";
String answer = "";
Scanner scan = new Scanner(System.in);
//**************************************
// Card1
//**************************************
System.out.println("Press 1 to evalaute your cards: ");
play = scan.nextInt();
while (play != 0){
System.out.println("First Card: ");
if (scan.hasNextInt())
{
card1 = scan.nextInt(9)+2;
}
else{
fc1 = scan.next();
switch(fc1)
{
case "A":
card1 = 14;
break;
case "K":
card1 = 13;
break;
case "Q":
card1 = 12;
break;
case "J":
card1 = 11;
break;
default:System.out.println("Incvalid entry");
}
//***************************************
// Card2
//***************************************
System.out.println("Second card: ");
if (scan.hasNextInt())
{
card2 = scan.nextInt(9) +2;
}
else{
fc2 = scan.next();
switch (fc2)
{
case "A":
card2 = 14;
break;
case "K":
card2 = 13;
break;
case "Q":
card2 = 12;
break;
case "J":
card2 = 11;
break;
default:System.out.println("Invalid entry.");
}
}
}
}
}
}
最佳答案
Allowing String
s in switch
statements was new in Java 1.7.将 Java 升级到 1.7+ 并在 Eclipse 中使用它,或者如果不能,则必须将案例转换为 if-else 语句。
关于java - 编译器不会让 String 变量出现在 switch 语句中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20364916/