主类:

public class Card
{
private String cardShort;
private String cardClas;
private String cardType;
public Card(String cardClas, String cardType)
{
    this.cardType = cardType;
    this.cardClas = cardClas;
}
public void cardSelector()
{
    switch(cardClas.toUpperCase())
    {
            case "J" : cardClas = "Jack";
                  break;
            case "K" : cardClas = "King";
                  break;
            case "Q" : cardClas = "Queen";
                  break;
            case "A" : cardClas = "Ace";
                  break;
            case "2" : cardClas = "Two";
                  break;
            case "3" : cardClas = "Three";
                  break;
            case "4" : cardClas = "Four";
                  break;
            case "5" : cardClas = "Five";
                  break;
            case "6" : cardClas = "Six";
                  break;
            case "7" : cardClas = "Seven";
                  break;
            case "8" : cardClas = "Eight";
                  break;
            case "9" : cardClas = "Nine";
                  break;
            case "10" : cardClas = "Ten";
                  break;
    }
    switch(cardType.toUpperCase())
    {
        case "S" : cardClas = "Spades";
                  break;
        case "C" : cardClas = "Clubs";
                  break;
        case "H" : cardClas = "Hearts";
                  break;
        case "D" : cardClas = "Diamonds";
                  break;
    }
}
public String getClasses()
{
    return cardType;
}
public String getType()
{
    return cardClas;
}
public String toString()
{
    String theory = getClasses() + " of " + getType();
    return theory;
}


}

测试人员:

import java.util.Scanner;
public class CardTester
{
public static void main(String[] args)
{
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter card: ");
    String shortHand = sc.next();

    if(shortHand.length() == 2)
    {
        Card c = new Card(shortHand.substring(0,1),shortHand.substring(1,2) );
        c.cardSelector();
        System.out.println(c.toString());
    } else if(shortHand.length() == 3)
    {
        Card c = new Card(shortHand.substring(0,2), shortHand.substring(2,3));
        c.cardSelector();
        System.out.println(c.toString());
    }
}


}

安慰:
“输入卡:

黑桃S

假设输出“黑桃A”。尝试了一堆不同的修复程序,不确定是什么原因导致了问题。

第一次使用开关盒,可能需要一些帮助...谢谢!

最佳答案

您的两个switch语句都设置了cardClas。您需要其中之一来设置cardType

另外,您的两个吸气剂也很令人困惑。您似乎已经以某种方式交换了姓名;因此您有getClasses返回cardTypegetType返回cardClas。您可能要解决此问题。

10-06 13:07