【问题标题】:while loop if statementswhile 循环 if 语句
【发布时间】:2015-05-19 08:49:11
【问题描述】:

我几乎已经完成了这个工作,但我无法获得:

if((pscore <= card1 +card2)) 语句循环直到玩家坚持或失败。

据我所知,它应该可以工作......但我错过了一个细节,我无法弄清楚是什么。

 import java.util.Random;
import java.util.Scanner;

class Blackjack
{
public static void main(String[] args)
{
    Random r = new Random();
    String name;
    Scanner scannerIn = new Scanner(System.in);

        boolean yourTurn = true;
        boolean dealersTurn =true;
        int card1 = 1 + r.nextInt(11);
        int card2 = 1 + r.nextInt(11);
        int dcard1 = 1 + r.nextInt(11);
        int dcard2 = 1 + r.nextInt(11);

        int pscore = card1 +card2;
        int dscore = dcard1 +dcard2;

        System.out.println("Welcome to Blackjack ! " );
        System.out.println("\nScore as close to 21 without going over to win ");
        System.out.println("\nWhat is your name?");
            name = scannerIn.nextLine();
        System.out.println("\nHello " + name);
        System.out.println("\nLet's play some BlackJack!");
            System.out.println("\nThe dealer shows:\t" +dcard1 );
        System.out.println("\n\nYour first card is:\t " +card1 );
        System.out.println("\nYour second card is:\t" +card2  );
            System.out.println("\nGiving you a grand total of: " +pscore );


    while (yourTurn)
    {
                if ((pscore <= +card1 +card2))
                System.out.println("\nWould you like to (H)it or (S)tick?");
                    String a = scannerIn.nextLine();
                if(a.toLowerCase().equals("h"))
        {
                int newCard = 1 + r.nextInt(11);
                System.out.println("\nYour next card is " +newCard );
                    pscore = pscore +newCard;
                System.out.println("\nGiving you a new total of "+pscore);

                if ((pscore >=22))
            {
                System.out.println("\nYou Busted! \nSorry! you lose");
                yourTurn = false;break;
            }
        }

        else if(a.toLowerCase().equals("s"))
        {   
            yourTurn = false;
            System.out.println("\nYou stick at " +pscore );
            System.out.println("\nNow it's the dealers turn\n Dealer must draw until 17");
        }
        else
        {
            System.out.println("\nPlease press H or S");
        }



    while (dealersTurn)
    {
        dealersTurn = true;
        {   if ((dscore <= dcard1+dcard2))
            System.out.println("\nThe dealers cards were:\n " +dcard1);
            System.out.println("\nand\n" +dcard2);
            System.out.println("\nGiving the Dealer a grand total of: " +dscore );
        }

        {
            int newCard1 = 1 + r.nextInt(11);
            if ((dscore<=17))
                System.out.println("\nThe dealer draws a: " +newCard1 );
            dscore = dscore +newCard1;
                System.out.println("\nGiving the dealer a grand total of: "+dscore);
        }
            if ((dscore >=22))
        {
            System.out.println("\nDealer Bust!");
            System.out.println("\nThe House loses");
            System.out.println("\nYou Win");
            dealersTurn = false;break;
        }
            else if ((dscore >=pscore))
        {
            System.out.println("\nDealer has " +dscore);
            System.out.println("\nThe dealer beat you!");
            System.out.println("\nThe House Wins!");
            dealersTurn = false;break;
        }
    }
    }
    scannerIn.close();
    }
    }

另外,我要感谢很多人,他们帮助我走到了这一步。如果有我找不到的人的 +1 按钮。

感谢您的帮助 文森特。

【问题讨论】:

  • 你的代码有多个错误,或许你应该先调试一下。

标签: java loops conditional-statements


【解决方案1】:

在 while(dealerTurn) 循环之后,您的 while(yourTurn) 循环才会关闭它的括号。这导致荷官转牌成为 yourTurn 循环的一部分。在dealersTurn while循环上方添加一个右括号,如下所示:

 }
 while (dealersTurn)
 {

然后从“scannerIn.close()”上方的底部删除旧的右括号

而且,你的逻辑的目的是什么

 if ((pscore <= +card1 +card2))

您的分数是 = 到 card1 + card2,然后如果您再抽一张牌,您的分数将大于那 2 张牌,因为您现在有 3 张牌。这就是为什么它也没有进入你的 if 语句。

【讨论】:

    猜你喜欢
    • 2013-06-09
    • 2014-01-21
    • 2015-11-08
    • 2016-01-12
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-02
    相关资源
    最近更新 更多