import java.util.Scanner;




public class Blackjack {

    class Commands {
        static final String yes = "yes";
        static final String no = "no";
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        //Opponent player2 = new Opponent();
        double AiCard1 = 0;
        double AiCard2 = 0;
        double AiCard3 = 0;
        double AiTotalcard3 = AiCard1 + AiCard2 + AiCard3;
        double card1 = 0;
        double card2 = 0;
        double card3 = 0;
        double card4 = 0;
        double total2 = card1 + card2 + card3 + card4;
        double total = card1 + card2 + card3;



        System.out.println("Hello and Welcome to my custom version of blackjack!");
        System.out.println("You will start off with $300");
        System.out.println();
        System.out.println("Do you want to read the rules before playing?");
        System.out.print("Press 1 if yes, press 2 for no");


            int choice = input.nextInt();
            switch (choice) {
            case 1:
                System.out.println("You have 4 cards to get as close to 21 as possible.  Whoever is closest to 21 wins");
                break;
            default:
                break;
        }




        int balance = 300;

        System.out.println("Your bank balance is $" + balance);
        //user places a bet
        System.out.println("Place a bet");

        int bet = input.nextInt();
        System.out.println("You placed a bet of " + bet);

        //this is the AI
        AiCard1 = Math.random() * 12 + 1;
        AiCard1 = Math.round(AiCard1);
        AiCard2 = Math.random() + 12 + 1;
        AiCard2 = Math.round(AiCard2);
        double AiTotal2cards = AiCard1 + AiCard2;
        if(AiTotal2cards < 15) {
            AiCard3 = Math.random() * 12 + 1;
            AiCard3 = Math.round(AiCard3);
            AiTotalcard3 = AiCard1 + AiCard2 + AiCard3;
        }



        card1 = Math.random() * 12 + 1;
        card1 = Math.round(card1);
        System.out.println("Your first card was a " + card1);
        System.out.println();

        card2 = Math.random() * 10 + 1;
        card2 = Math.round(card2);

        System.out.println("Your second card was a " + card2);
        System.out.println();

        System.out.println("Your cards add up to " + (card1 + card2));
        System.out.println("Do you want to add another card?");
        String answer = input.next();
        here:
        if(answer.equals(Commands.yes)) {

            card3 = Math.random() * 12 + 1;
            card3 = Math.round(card3);
            System.out.println("Your third card was a " + card3);
            System.out.println("Your cards add up to " + (card1 + card2 + card3));
            total = card1 + card2 + card3;

            if(total > 21) {
                System.out.println("You lose");
                break here;
            }
            System.out.println("Do you want to add another card?");


            String answer1 = input.next();
            if(answer1.equals(Commands.no)){
                break here;
            }
            if(answer1.equals(Commands.yes)) {

                card4 = Math.random() * 12 + 1;
                card4 = Math.round(card4);
                System.out.println("Your fourth card was a " + card4);
                System.out.println("Your cards add up to " + (card1 + card2 + card3 + card4));
                total2 = card1 + card2 + card3 + card4;

                if(total2 > 21) {
                    System.out.println("You lose");
                    break here;
                }
                break here;
            }
        }
        System.out.println("Your total cards were " + total2);
        System.out.println("AI total cards were " + AiTotalcard3);
        input.close();

    }
}


当您运行程序时,我希望当用户浏览程序时,顶部的变量中将保存新的数字。有没有更好的方法来执行此操作,因为控制台输出的总值始终为0。

最佳答案

首先让我们看一下AiTotalcard3

它不应该总是为此输出0,更确切地说,在AiCard1 + AiCard2 >= 15的情况下它将输出0,这可能经常发生。

如果您跟踪您的实际代码,这是有意义的。发生的第一件事是这些初始化程序:

double AiCard1 = 0;
double AiCard2 = 0;
double AiCard3 = 0;
double AiTotalcard3 = AiCard1 + AiCard2 + AiCard3;


因此AiTotalcard3最初设置为0。在您更改它之前,它将一直保持0,这是在这里发生的:

AiCard1 = Math.random() * 12 + 1;
AiCard1 = Math.round(AiCard1);
AiCard2 = Math.random() + 12 + 1;
AiCard2 = Math.round(AiCard2);
double AiTotal2cards = AiCard1 + AiCard2;
if(AiTotal2cards < 15) {
    AiCard3 = Math.random() * 12 + 1;
    AiCard3 = Math.round(AiCard3);
    AiTotalcard3 = AiCard1 + AiCard2 + AiCard3;
}


但这仅在前两张卡的总和少于15时才会发生。此后,您再也无需触摸AiTotalcard3了。

您的逻辑可以被大大简化,并且在卡片上使用int而不是double更加有意义,但是忽略所有这些并保持当前的风格,可以通过确保所有内容来解决此问题。路径设置为AiTotalcard3,例如(同样不是最简洁的逻辑,只是坚持使用您的模式):

if(AiTotal2cards < 15) {
    AiCard3 = Math.random() * 12 + 1;
    AiCard3 = Math.round(AiCard3);
    AiTotalcard3 = AiCard1 + AiCard2 + AiCard3;
} else {
    AiTotalcard3 = AiCard1 + AiCard2; // be sure to always set it
}


现在,考虑到所有这些,您在total2上也遇到类似的问题:并非所有的执行路径都设置total2的值。如果将其补足到第4张卡,它会起作用,但在此之前,您永远不要更改其初始值0。因此,您只需要用一把细齿梳子梳理一下即可。

您的基本问题之一是由于某种原因,您在前两张纸牌与所有纸牌中到处使用了不同的变量。如果仅使用一个aiTotaltotal来累积卡总数,则可以避免大多数此类问题。

10-06 06:22