制作酒杯程序,遇到麻烦的方法返回一个数组
问题描述:
代码:https://gist.github.com/anonymous/71d1baf86eb8354cfbfe制作酒杯程序,遇到麻烦的方法返回一个数组
当我运行它,我得到这个问题:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
This method must return a result of type int[]
at NewBlackJack.genCards(NewBlackJack.java:25)
at NewBlackJack.main(NewBlackJack.java:21)
我想不通的生活我最新错了。我知道这与返回数组有关,我已经完成了我的研究,但我仍然需要帮助。
谢谢!
答
,当你进入if语句
if (genWhat == 0){
当genWhat!= 0你不回任何东西你只能返回。这就是为什么它在抱怨。所以你需要在if语句之后添加一个return语句。
答
根据你的代码似乎因为你的代码通过0到你的方法应该这样写genCards你应该简单地删除if语句:
public static int[] genCards(int genWhat)
{
Random r = new Random();
/*if (genWhat == 0) remove this if statement
{ */
int[] cards = {0,0,0,0,0,0};
for (int i = 0; i < 4; i += 1)
{
cards[i] = 2 + r.nextInt(13);
if (cards[i] > 11)
{
cards[i] = 10;
}
if (cards[i] == 11)
{
if (cards[i] < 2)
{
cards[5] += 1;
}
else
{
cards[6] += 1;
}
}
}
System.out.println(cards);
return cards;
//}
}
+0
如果此答案有用,请您批准我的答案? :d – rnm20 2013-03-19 23:57:02
谢谢!我知道这会是这样的愚蠢! – user2155333 2013-03-20 00:45:12