【发布时间】:2016-10-05 12:02:38
【问题描述】:
基本上我有这个代码,输入 2 个 OR 3 个字母/数字组合,如(AH、10D、JS 和 7C)作为花色值(红桃 A、方块10,黑桃J,梅花7。)
为什么,如果我输入“10D”,代码什么也没有打印出来?为什么不是注册10?我怎样才能解决这个问题? 还:请帮我弄清楚如何将案例 2 - 案例 10 的范围缩短为更有说服力的内容?我不能使用 if-then-else 语句。
System.out.print("Please enter a letter/integer of a playing card (A, J, Q, K, or 2 - 10),\nfollowed by card type (D, H, S, C):");
Scanner kbd = new Scanner(System.in);
String userInput = kbd.next().toUpperCase();
String valueofCard = userInput.substring(0, userInput.length() / 2); // gives first half of string
String suitofCard = userInput.substring(userInput.length() / 2); //give last half of string with + 1 if odd
StringBuilder result = new StringBuilder();
switch (valueofCard) {
case "A":
result.append("Ace of ");
break;
case "J":
result.append("Jack of ");
break;
case "Q":
result.append("Queen of ");
break;
case "K":
result.append("King of ");
break;
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
case "10":
result.append(valueofCard + " of ");
break;
}
switch (suitofCard) {
case "D":
result.append("Diamonds");
break;
case "H":
result.append("Hearts");
break;
case "S":
result.append("Spades");
break;
case "C":
result.append("Clubs");
break;
}
System.out.println(result.toString());
kbd.close();
【问题讨论】:
-
根据您的解释,输入的长度将是 2 或 3 个字符,那么为什么不这样编码呢?
-
你是什么意思?这是指向另一个人的问题的链接,该问题说明了我的确切周长,也许(?)解释了为什么我以这种格式编码。 stackoverflow.com/questions/39834840/…
标签: java split playing-cards