【问题标题】:Method return type error [duplicate]方法返回类型错误[重复]
【发布时间】:2018-09-26 19:31:35
【问题描述】:

我正在使用一种方法在我的 Black Jack 程序中发牌。这是我的方法:

 public static String dealCard() {
    Random randomCard = new Random();
    int min = 1;
    int rCard = min + randomCard.nextInt(13);
    switch(rCard) {
    case 1 : return "2"; 
    case 2 : return "3";
    case 3 : return "4";
    case 4 : return "5";
    case 5 : return "6";
    case 6 : return "7";
    case 7 : return "8";
    case 8 : return "9";
    case 9 : return "10";
    case 10 : return "Queen";
    case 11 : return "Jack";
    case 12 : return "King";
    case 13 : return "Ace";
    }
 }

但是我收到以下错误...

This method must return a result of type String.

这就是为什么我很困惑,因为当我选择箱子然后它返回卡片时,我不是返回一个字符串吗?我很困惑。我尝试切换我的方法并尝试使用 If 语句而不是 switch case,但我仍然得到相同的方法。我怀疑它是我的nextInt(); 函数的一部分,但我不知道是什么。如有任何关于我可能出错的地方的建议,我将不胜感激。

【问题讨论】:

  • 如果 rCard = 20 会怎样? (编译器不知道这是不可能的)——那么你的方法会返回什么?
  • 与实际问题无关:我不做Java但不是randomCard.nextInt(13) 意味着随机数上限为13(12 + 1)? 20 的结果应该是不可能的。
  • @Steve 编译器不知道这一点。这就是评论的重点
  • 此外,这正是需要 Map 的情况,甚至只是一个字符串数组。
  • 与原问题无关,但我想问一下,Jack不应该在Queen之前吗?哦,也许你在想“女士优先”:-P

标签: java switch-statement blackjack


【解决方案1】:

在您的switch 语句中添加default:

public static String dealCard() {
    Random randomCard = new Random();
    int min = 1;
    int rCard = min + randomCard.nextInt(12);
    String cardDealt = "";
    switch (rCard) {
        case 1:
            cardDealt = "Ace";
            break;
        case 2:case 3:case 4:case 5:case 6:case 7:case 8:case 9:
            cardDealt = Integer.toString(rCard);
            break;
        case 10:
            cardDealt = "Jack";
            break;
        case 11:
            cardDealt = "Queen";
            break;
        case 12:
            cardDealt = "King";
            break;
        default:
            throw new IllegalArgumentException("rCard unexpectedly out of range");
    }
    return cardDealt;
}

【讨论】:

  • 虽然你我都知道它永远不会被使用,但编译器还不够聪明,无法解决这个问题。
【解决方案2】:

您仍然需要在可能的情况之外返回值。

此外,虽然简单的 if-else 看起来更简洁,但您可以使用 fall through case 语句

switch(rCard) {
    case 10 : return "Queen";
    case 11 : return "Jack";
    case 12 : return "King";
    case 13 : return "Ace";
    case 1 :
    case 2 :
    case 3 : 
    case 4 : 
    case 5 : 
    case 6 : 
    case 7 : 
    case 8 : 
    case 9 : return String.valueOf(rCard + 1);
    default: return null;
}

【讨论】:

    【解决方案3】:

    编译器无法确定您的case 语句之一将被命中,因此您将return 一个值。 必须返回一个值。您可以将default 案例添加到switch。或者,只需将您的最后一个 return 移动到最后一个语句。此外,在每次调用时重新初始化 Random 也是一个坏主意。在一个循环中,您可能得到相同的种子。

    private static Random randomCard = new Random();
    private static final int MIN = 1; // <-- this is a CONSTANT
    private static final int MAX = 13; // <-- this is a CONSTANT
    public static String dealCard() {
        int rCard = MIN + randomCard.nextInt(MAX);
        switch(rCard) {
        case 1 : return "2"; 
        case 2 : return "3";
        case 3 : return "4";
        case 4 : return "5";
        case 5 : return "6";
        case 6 : return "7";
        case 7 : return "8";
        case 8 : return "9";
        case 9 : return "10";
        case 10 : return "Queen";
        case 11 : return "Jack";
        case 12 : return "King";
        }
        return "Ace";
    }
    

    可以使用MapgetOrDefault 进一步优化它(imo)

    private static Random randomCard = new Random();
    private static final int MIN = 1; // <-- this is a CONSTANT
    private static final int MAX = 13; // <-- this is a CONSTANT
    private static final Map<Integer, String> faceCards = new HashMap<>();
    static {
        faceCards.put(11, "Jack");
        faceCards.put(10, "Queen");
        faceCards.put(12, "King");
        faceCards.put(13, "Ace");
    }
    
    public static String dealCard() {
        int rCard = MIN + randomCard.nextInt(MAX);
        return faceCards.getOrDefault(rCard, String.valueOf(rCard + 1));
    }
    

    【讨论】:

    • 离题 - 使用静态块初始化常量映射是否被认为是好的做法?
    • @cricket_007 拥有全局变量(完全)通常不被认为是最佳实践。在这里,我正在初始化一个 private static 字段。下一个合乎逻辑的地方是enum
    【解决方案4】:

    此代码无法编译,因为如果 rCard 小于 1 或大于 13,则您错过了 return 语句。 您有 2 个选项来解决此问题:

    • 在开关中添加默认选项
    • 在方法结束时抛出 IllegalStateException

    所以,最终的代码应该是这样的:

    public static String dealCard() {
        Random randomCard = new Random();
        int min = 1;
        int rCard = min + randomCard.nextInt(13);
        switch(rCard) {
            case 1 : return "2";
            case 2 : return "3";
            case 3 : return "4";
            case 4 : return "5";
            case 5 : return "6";
            case 6 : return "7";
            case 7 : return "8";
            case 8 : return "9";
            case 9 : return "10";
            case 10 : return "Queen";
            case 11 : return "Jack";
            case 12 : return "King";
            case 13 : return "Ace";
        }
        throw new IllegalStateException("No return value fo case " + rCard);
    }
    

    【讨论】:

      【解决方案5】:

      更多使用枚举的优雅和优化方式:

       public enum Card {
          TWO(2, "2"),
          THREE(3, "3"),
          FOUR(4, "4"),
          FIVE(5, "5"),
          SIX(6, "6"),
          SEVEN(7, "7"),
          EIGHT(8, "8"),
          NINE(9, "9"),
          TEN(10, "10"),
          QUEEN(11, "Queen"),
          JACK(12, "Jack"),
          KING(13, "King"),
          ACE(14, "Ace");
      
          private final int value;
          private final String name;
      
          private static final Card[] VALUES = values();
          private static final int SIZE = VALUES.length;
          private static final Random RANDOM = new Random();
      
          Card(int value, String name) {
              this.value = value;
              this.name = name;
          }
      
          public static Card getRandomCard() {
              return VALUES[RANDOM.nextInt(SIZE)];
          }
      
          public int getValue() {
              return value;
          }
      
          public String getName() {
              return name;
          }
      }
      
      public static void main(String[] args) {
          System.out.println(Card.getRandomCard().getName());
      }
      

      【讨论】:

        猜你喜欢
        • 2015-05-16
        • 1970-01-01
        • 1970-01-01
        • 2013-12-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-27
        相关资源
        最近更新 更多