首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >while循环if语句

while循环if语句
EN

Stack Overflow用户
提问于 2015-03-17 10:35:12
回答 1查看 49关注 0票数 0

我就快把这玩意儿弄好了,我就是搞不定:

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

据我所知,它应该是有效的。但我漏掉了一个细节我不知道是什么。

代码语言:javascript
运行
复制
 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的人按钮,我找不到它。

谢谢你的帮助文森。

EN

回答 1

Stack Overflow用户

发布于 2015-03-17 10:41:01

您的while(yourTurn)循环直到while(dealerTurn)循环之后才结束它的括号。这导致经销商成为yourTurn循环的一部分。在dealersTurn while循环上方添加一个右括号,如下所示:

代码语言:javascript
运行
复制
 }
 while (dealersTurn)
 {

然后从"scannerIn.close()“上方的底部删除旧的结束括号。

还有,你的逻辑的目的是什么?

代码语言:javascript
运行
复制
 if ((pscore <= +card1 +card2))

你的分数是= to card1 + card2,如果你抽出另一张牌,你的分数将大于这两张牌,因为你现在有3张牌。这就是为什么它也没有输入if语句的原因。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29090381

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档