首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我如何使用if语句来知道是玩家还是计算机赢了?

问我如何使用if语句来知道是玩家还是计算机赢了?
EN

Stack Overflow用户
提问于 2019-03-23 05:59:55
回答 2查看 88关注 0票数 1

我刚刚开始学习java,所以我可能还没有走上正确的道路,但我有一个任务,要求我创建一个21人的游戏。这个游戏的运行方式是玩家和计算机轮流输入1、2或3。输入一个满足或超过21的数字的玩家输。我似乎遇到的麻烦是,当输入最后一个数字时,我似乎不能让程序退出循环,它将显示玩家每次都会输,无论是赢是输。

我尝试过在do-while循环之后使用另一个if语句来显示"You Win!“或者“你输了”但是,我不知道应该在if语句中使用哪些参数来决定谁赢了。我也试着将游戏设置为玩家为偶数,计算机为奇数,但我无法将这些数字添加到运行总数中以结束循环。

代码语言:javascript
复制
  int numLoops = 0;
  int firstCheck;
  int points;
  Scanner input = new Scanner(System.in);

  do
  {
     System.out.print("\nEnter a 1, 2, or 3 >> ");
     points = input.nextInt();

     int random = (int )(Math.random() *  3 + 1);
     numLoops = points + numLoops + random;

     if(numLoops < 21)
     {
        System.out.println("The computer entered a " + random);         
        System.out.println("The new total is " + numLoops);
     }
     else
     {
        //This is what always prints.
        System.out.println("You lost! The computer is the victor.");
     }
  }while(numLoops < 21);
  //this is what I am having most of my trouble with.
  System.out.println("You Win!");

我预计循环将在总数达到21之后关闭,并将输出一条语句,该语句将根据谁获胜而有所不同。但是,程序总是输出玩家丢失的内容。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-03-23 06:31:09

如果我做了一些改变来简化事情。看一下源代码,它应该有很好的文档,并且解释得很好。

代码语言:javascript
复制
/*
 * Which players turn is it?
 * (true for human player, false for computer)
 */ 
boolean isHumanPlayersTurn = true;
int totalPoints = 0;

Scanner input = new Scanner(System.in);

// the game ending condition
while (totalPoints < 21) {

    // take an action, depending on the players turn
    if (isHumanPlayersTurn) {
        System.out.print("\nEnter a 1, 2, or 3 >> ");
        totalPoints += input.nextInt();
    } else {
        int random = (int) (Math.random() * 3 + 1);
        System.out.println("The computer takes " + random);
        totalPoints += random;
    }

    System.out.println("Total amount is " + totalPoints);

    /*
     * Important part: After each players move, change the players turn, but do NOT
     * do this if the game already ended, since then the other player would have won.
     */
    if (totalPoints < 21) {
        isHumanPlayersTurn = !isHumanPlayersTurn;
    }
}

// now, the player to move is the one who loses
if (isHumanPlayersTurn) {
    System.out.println("You lost! The computer is the victor.");
} else {
    System.out.println("You Win!");
}
票数 0
EN

Stack Overflow用户

发布于 2019-03-23 06:57:11

在用户输入数字后,您应该立即检查他们是否达到21 (并输出该点的总数)。如果21没有命中,那么计算机应该猜测,然后您再次检查是否命中了21。因此,您将有两个if语句(每次猜测后一个;user/computer)。在else块中,用户猜测是计算机要猜测的位置。您的if语句将检查>= 21,指示用户或计算机是否丢失。您不需要在循环外声明胜利器,因为这可以在循环内完成。

例如:

代码语言:javascript
复制
    int total = 0;
    do
    {
        System.out.print("\nEnter a 1, 2, or 3 >> ");
        points = input.nextInt();
        total = total + points;
        System.out.println("The new total is " + total);

        if (total >=21)
        {
            System.out.println("You lost! The computer is the victor.");
        }
        else
        {
            int random = (int )(Math.random() *  3 + 1);
            total = total + random;
            System.out.println("The computer entered a " + random);         
            System.out.println("The new total is " + total);

            if(total >= 21)
            {
                System.out.println("You Win!");
            }
        }
    }while(total < 21);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55308324

复制
相关文章

相似问题

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