2
votes

The code: https://gist.github.com/anonymous/71d1baf86eb8354cfbfe

When I run it, I get this problem:

    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)

I can't figure out for the life of me whats wrong. I know it has something to do with returning an array, and I've done my research, but I still need help.

Thanks!

2

2 Answers

2
votes

You only return when you enter the if statement

if (genWhat == 0){

When genWhat != 0 you don't return anything at all. That is why it is complaining. So you need to add a return statement after the if statement.

0
votes

According to your code it seems you should simply remove the if statement since your code passes 0 to genCards your method should be written like this:

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;
        //}
    }