Search code examples
javareturn

java: missing return statement for blackjack game


public static String determiner(String userResponse, int playerHand, int AiHand) {
    if (userResponse.equals("hit") && playerHand < 21 && AiHand <21) {
        int RandomGen = (int) (Math.random()*11 + 1);
        int newHand = playerHand + RandomGen;
        int AIRandomGen = (int) (Math.random()*11 + 1);
        int newAIHand = AiHand + AIRandomGen;
        return "Your current hand is " + newHand;
    }
    else if (userResponse.equals("stay") && playerHand > 21 && AiHand < 21) {
        if (playerHand > AiHand) {
             String x = "You won!";
             return x;
        }
        else if (AiHand > playerHand) {
            String x =  "Sorry, you lost.";
            return x;
        }
        else if (playerHand == 21) {
            String x = "You won! You hit the jackpot :0";
            return x;
        }
        else if (AiHand == 21) {
            String x = "Sorry, you lost. Enemy hit the jackpot";
            return x;
        }
    }
    else if (userResponse.equals("hit") && playerHand > 21) {
        String x =  "Game over, you lost. You went boom boom";
        return x;
    }
    else {
        return null;
    }
}

I've taken a look at other questions but they don't seem to be helping. I dont' know what is wrong with my code. Can someone help me? THis is supposed to be a function that checks if the player's hand is over 21 and stuff like that (Blackjack game).


Solution

  • else if (AiHand == 21) {
            String x = "Sorry, you lost. Enemy hit the jackpot";
            return x;
    }
    

    Imagine what if AiHand is not 21 here: it'll go on to the end without returning anything.

    Fix this else if.