I made my first attempt at blackjack java code but the method is long. What should I do?

330    Asked by AnilJha in Java , Asked on Oct 6, 2022

I just completed my first multi class program, Blackjack, and it works! It allows the user to play Blackjack against a single dealer, with no other players at the table. I was wondering, seeing as it is my first multi class program, if you could help me optimise it and/or give me any advice. I want to implement insurance and splitting, so any advice to help prepare the code for eventually adding those features would be really helpful! Finally, my main method is pretty long — I was wondering if this is typical of Java programs and, if not, how I can fix that.


Answered by Amit raj

Return the boolean directly in the blackjack java code


The following:
public static boolean isyesorno(String answer)
{
    if(answer.equals("yes") || answer.equals("no"))
    {
        return true;
    }
    return false;
}
should become:
public static boolean isyesorno(String answer) {
    return answer.equals("yes") || answer.equals("no"))
}

The same goes for public static boolean hasBlackJack(int handValue) and public static boolean isHitorStand(String hitter) and public static boolean checkBust(int handvalue) for the latter you should move printing out of the function.

Use already existing wheels

You can shuffle the deck by using the built-in:
List list = Arrays.asList(deck);
Collections.shuffle(list);


Your Answer

Interviews

Parent Categories