缺少返回类型编译错误

时间:2014-04-06 21:13:12

标签: java compiler-errors

我有一个toString方法,带有一堆嵌套的if语句。我想我的一切都是正确的,但我得到了一个错误的退货声明"错误。谁能明白为什么?感谢。

public String toString() //to tell the user what card/s they have
//c.toString()
{
    //for printing?//
//  ArrayList<String> forPrint = new ArrayList<String>();
    getSuit();
    getValue();
    for(int i = 0; i < 5; i++)
    {
        if(suit == 1)
        {
            if(value == 11)
            {
                return "Jack of Clubs";
            //  forPrint.add("Jack of Clubs");
            }
            else if(value == 12)
            {
                return "Queen of Clubs";
            //  forPrint.add("Queen of Clubs"); 
            }
            else if(value == 13)
            {
                return "King of Clubs";
            //  forPrint.add("King of Clubs");  
            }
            else if(value == 1)
            {
                return "Ace of Clubs";
            //  forPrint.add("Ace of Clubs")    
            }
            else
            {
                return value + " of Clubs";
            //  forPrint.add(value + " of Clubs");
            }   
        }

        else if(suit == 2)
        {
            if(value == 11)
            {
                return "Jack of Diamonds";
            //  forPrint.add("Jack of Diamonds");   
            }
            else if(value == 12)
            {
                return "Queen of Diamonds";
            //  forPrint.add("Queen of Diamonds");  
            }
            else if(value == 13)
            {
                return "King of Diamonds";
            //  forPrint.add("KIng of Diamonds");   
            }
            else if(value == 1)
            {
                return "Ace of Diamonds";
            //  forPrint.add("Ace of Diamonds");    
            }
            else{
                return value + "of Diamonds";
            //  forPrint.add(value + " of Diamonds");
            }
        }

        else if(suit == 3)
        {
            if(value == 11)
            {
                return "Jack of Hearts";
            //  forPrint.add("jack of Hearts"); 
            }
            else if(value == 12)
            {
                return "Queen of Hearts";
            //  forPrint.add("Queen of Hearts");
            }
            else if(value == 13)
            {
                return "King of Hearts";
            //  forPrint.add("King of Hearts");
            }
            else if(value == 1)
            {
                return "Ace of Hearts";
            //  forPrint.add("Ace of Hearts");
            }
            else
            {
                return value + "of Hearts";
            //  forPrint.add(value + " of Hearts");
            }
        }

        else if(suit == 4)
        {
            if(value == 11)
            {
                return "Jack of Spades";
            //  forPrint.add("Jack of Spades"); 
            }
            else if(value == 12)
            {
                return "Queen of Spades";
            //  forPrint.add(   
            }
            else if(value == 13)
            {
                return "King of Spades";
            }
            else if(value == 1)
            {
                return "Ace of Spades";
            }
            else
            {
                return value + "of Spades";
            }
        }
        else{
            return "none";
        }       //this will never happen

    }

}

4 个答案:

答案 0 :(得分:2)

return "none";

在方法的最后。

答案 1 :(得分:2)

你需要把它放在那里

        //else{
        //    return "none";
        //}       //this will never happen

    }
    return "none"; //Guess it won't happen too
}

答案 2 :(得分:2)

我将您的逻辑简化为方法,并使用StringBuilder,例如 -

public static String getCardName(int value, int suit) {
  StringBuilder sb = new StringBuilder();
  if (value == 1) {
    sb.append("Ace");
  } else if (value == 11) {
    sb.append("Jack");
  } else if (value == 12) {
    sb.append("Queen");
  } else if (value == 13) {
    sb.append("King");
  } else {
    sb.append(value);
  }
  sb.append(" of ");
  if (suit == 1) {
    sb.append("Clubs");
  } else if (suit == 2) {
    sb.append("Diamonds");
  } else if (suit == 3) {
    sb.append("Hearts");
  } else {
    sb.append("Spades");
  }
  return sb.toString();
}

答案 3 :(得分:0)

有一种可能性,if-blocks不执行。这就是“缺少返回类型”编译错误的原因。如果控件永远不会进入for循环,会发生什么。要解决此问题,请在return - 循环结束的位置旁边添加for语句。