dimanche 19 juillet 2020

How can I fix this if statement in my blackjack bust system?

So i'm new to coding and working on an assignment for school. I'm coding blackjack with Javascript. this is probably an easy fix but my code does this


The dealers total is 7

Your total is 11

would you like another card? y/n y


Your total is 20 Would you like another card? y/n y


Your total is 26 Would you like another card? y/n y


Your total is 26 You went bust You now have $400.0 Would you like to play again? y/n

.

the problem is that it gets higher than 21 but doesn't tell you that you went bust until the next loop. I'd like it to just say the last one instead of the 2nd to last and the last one.

System.out.println("would you like another card? \ny/n");
answer = sanswer.nextLine();

//if player answers
while ("y".equals(answer)) {
    if (total < 21){
        hit = (int) Math.floor(Math. random() * 10) + 1;
        total += hit;
        System.out.println("____________________________\n");
        System.out.println("Your total is " + total);
        System.out.println("Would you like another card? \ny/n");
        answer = sanswer.nextLine();
    }  
    //player bust
    if (total > 21) {
        System.out.println("______________");
        System.out.println("Your total is " + total);
        System.out.println("You went bust");     
        System.out.println("You now have $" + money);
        play = "no";
        System.out.println("Would you like to play again? y/n");
        answer = "";
        play = srplay.next();
    }
}

Aucun commentaire:

Enregistrer un commentaire