Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用if语句来确定播放机或计算机是否获胜?_Java_Loops_If Statement - Fatal编程技术网

Java 如何使用if语句来确定播放机或计算机是否获胜?

Java 如何使用if语句来确定播放机或计算机是否获胜?,java,loops,if-statement,Java,Loops,If Statement,我刚刚开始学习java,所以我甚至可能没有走上正确的轨道,但我有一个任务,要求我创建一个21人的游戏。游戏的工作方式是玩家和计算机轮流输入1、2或3。输入的数字达到或超过21的玩家将失败。我似乎遇到的麻烦是,当输入最终数字时,我似乎无法让程序退出循环,它会显示玩家每次都输,无论是赢还是输 我曾尝试在do while循环之后使用另一个if语句来显示“You Win!”或“You Lost”。但是,我不知道应该在if语句中使用哪些参数来决定谁赢了。我还尝试将玩家设置为偶数,计算机设置为奇数,但我无法

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

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

  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!");
int numLoops=0;
int firstCheck;
积分;
扫描仪输入=新扫描仪(System.in);
做
{
系统输出打印(“\n输入1、2或3>”;
points=input.nextInt();
int random=(int)(Math.random()*3+1);
numLoops=点数+numLoops+随机数;
如果(numLoops<21)
{
System.out.println(“计算机输入了”+随机数);
System.out.println(“新总数为”+numLoops);
}
其他的
{
//这是经常打印的。
System.out.println(“你输了!计算机是胜利者。”);
}
}而(numLoops<21);
//这是我最头疼的问题。
System.out.println(“你赢了!”);

我预计循环将在总数达到21后结束,并将输出一个根据谁获胜而有所不同的声明。然而,程序总是输出玩家丢失的信息。

如果你做了一些更改来简化事情的话。看一看源代码,它应该有很好的文档记录,并最好地解释事情

/*
 * 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!");
}
/*
*轮到哪个球员了?
*(对于人类玩家为true,对于计算机为false)
*/ 
布尔值isHumanPlayersTurn=true;
int totalPoints=0;
扫描仪输入=新扫描仪(System.in);
//游戏结束条件
而(总积分<21){
//根据玩家的回合数采取行动
如果(球员轮换){
系统输出打印(“\n输入1、2或3>”;
totalPoints+=input.nextInt();
}否则{
int random=(int)(Math.random()*3+1);
System.out.println(“计算机取”+随机数);
总点数+=随机数;
}
System.out.println(“总金额为”+totalPoints);
/*
*重要部分:在每个玩家移动后,改变玩家的回合,但不要改变
*如果游戏已经结束,那么就这样做,因为那时其他玩家会赢。
*/
如果(总点数<21){
isHumanPlayersTurn=!isHumanPlayersTurn;
}
}
//现在,要移动的玩家是输球的玩家
如果(球员轮换){
System.out.println(“你输了!计算机是胜利者。”);
}否则{
System.out.println(“你赢了!”);
}

如果您已经做了一些更改来简化事情。看一看源代码,它应该有很好的文档记录,并最好地解释事情

/*
 * 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!");
}
/*
*轮到哪个球员了?
*(对于人类玩家为true,对于计算机为false)
*/ 
布尔值isHumanPlayersTurn=true;
int totalPoints=0;
扫描仪输入=新扫描仪(System.in);
//游戏结束条件
而(总积分<21){
//根据玩家的回合数采取行动
如果(球员轮换){
系统输出打印(“\n输入1、2或3>”;
totalPoints+=input.nextInt();
}否则{
int random=(int)(Math.random()*3+1);
System.out.println(“计算机取”+随机数);
总点数+=随机数;
}
System.out.println(“总金额为”+totalPoints);
/*
*重要部分:在每个玩家移动后,改变玩家的回合,但不要改变
*如果游戏已经结束,那么就这样做,因为那时其他玩家会赢。
*/
如果(总点数<21){
isHumanPlayersTurn=!isHumanPlayersTurn;
}
}
//现在,要移动的玩家是输球的玩家
如果(球员轮换){
System.out.println(“你输了!计算机是胜利者。”);
}否则{
System.out.println(“你赢了!”);
}

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

例如:

    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);
int-total=0;
做
{
系统输出打印(“\n输入1、2或3>”;
points=input.nextInt();
总数=总数+分数;
System.out.println(“新总数为”+总数);
如果(总数>=21)
{
System.out.println(“你输了!计算机是胜利者。”);
}
其他的
{
int random=(int)(Math.random()*3+1);
总数=总数+随机数;
System.out.println(“计算机输入了”+随机数);
System.out.println(“新总数为”+总数);
如果(总数>=21)
{
System.out.println(“你赢了!”);
}
}
}总数<21人;

用户输入数字后,应立即检查数字是否达到21(并输出该点的总数)。如果21没有被击中,那么计算机应该猜测,然后再次检查21是否被击中。因此,您将有两个if语句(每个猜测后一个;用户/计算机)。在用户猜测后的else块中,计算机