使用switch语句进行扑克牌程序(Java)

时间:2016-10-04 23:28:51

标签: java switch-statement

到目前为止,我已经找到了我的程序,只是因为我不理解我给出的这些指令(或者至少理解如何执行它们)。

当我键入10时,它打印出“10”,但是当我尝试为10个黑桃键入10S时,它只打印出“黑桃”。

希望有人在这里可以给我一个解决方案,或指出我如何解决我的问题的正确方向:

  

使用SWITCH语句为结果变量分配初始值 - 卡的值

     

使用第二个SWITCH语句将结果变量连接到卡片的“

这是代码:

import java.util.*;


public class CardConverter {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);

        //will hold string that user will input
        String card, face, suit, result;

        //getting input from user and telling them correct format
        System.out.println("Please enter either the number or face value intial of a card followed by the initial of it's suit (ie. QH for Queen of Hearts)");
        card = keyboard.nextLine();
        //gets first value
        face = card.substring(0);
        //sets substring for 10 only
        //substring for after all single digit/letter card faces
        suit = card.substring(1);


        //to print face and word of
        switch (face)
        {
        case "10":
            System.out.println("10 of ");
            break;
        case "2":
            System.out.println("2 of ");
            break;  
        case "3":
            System.out.println("3 of ");
            break;
        case "4":
            System.out.println("4 of ");
            break;  
        case "5":
            System.out.println("5 of ");
            break;
        case "6":
            System.out.println("6 of ");
            break;
        case "7":
            System.out.println("7 of ");
            break;
        case "8":
            System.out.println("8 of ");
            break;
        case "9":
            System.out.println("9 of ");
            break;
        case "J":
            System.out.println("Jack of ");
            break;
        case "Q":
            System.out.println("Queen of ");
            break;
        case "K":
            System.out.println("King of ");
            break;
        case "A":
            System.out.println("Ace of ");
            break;  
        }   
        //to print out card suit
        switch (suit)
            {
            case "H":
                    System.out.println("Hearts");
                break;
            case "C":
                System.out.println("Clubs");
                break;  
            case "S":
                System.out.println("Spades");
                break;
            case "D":
                System.out.println("Diamonds");
                break;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您的问题从card.substring(0);开始,等于card,因为字符串开头的子字符串。也许你想要card.charAt(0);?但这也是错误的,因为"10S"将有三个字符,两个用于面值。

您需要专门处理三个字符的输入,或者更明智地处理substring

您知道套装将始终是最后一个字符,因此请使用字符串的长度charAt

int suitIndex = s.length() - 1;
String suit = ""+s.charAt(suitIndex);
String face = s.substring(0,suitIndex);

您还可以简化案例

case "J":
    System.out.println("Jack of ");
    break;
case "Q":
    System.out.println("Queen of ");
    break;
case "K":
    System.out.println("King of ");
    break;
case "A":
    System.out.println("Ace of ");
    break;  
default:
    System.out.println(face + " of "); // handle all the numbers
    break;