【问题标题】:Variables set to true wont go into while loop java设置为 true 的变量不会进入 while 循环 java
【发布时间】:2017-05-15 04:29:03
【问题描述】:

我需要一些帮助来解决在我的代码中编写布尔表达式的问题

import java.util.*;
import java.util.concurrent.ThreadLocalRandom;

public class Dudo

{

    public static void main(String[] args) 
    {
        // raise call  spot on  pass
        Scanner reader = new Scanner (System.in);
        boolean player1;
        boolean player2;        
        int dicep1 = 5;
        int dicep2 = 5;
        Random rand = new Random();
        int  n = rand.nextInt(6) + 1;
        int  y = rand.nextInt(6) + 1;
        System.out.println("Welcome to Dudo!");
        System.out.println("Rolling to see who goes first");
        {

            if (n > y )
            {
                System.out.println("Player 1 goes first");  
            }
            else if (n < y)
            {
                System.out.println("Player 2 goes first");
            }
            else if (n == y)
            {
                System.out.println("Player 2 goes first");
            }
            int[] Array;
            Array = new int [dicep1];
            for(int i=0;i<Array.length;i++)
            {
                Array[i] = randomFill();
            }

            int[] Array2;
            Array2 = new int [dicep2];
            for(int j=0;j<Array2.length;j++)
            {
                Array2[j] = randomFill2();
            }
            player1 = true;
            player2 = true;


            while ((player1 == true) && (player2 == true));
            {
                System.out.println("Player 1, your numbers are....." + Arrays.toString(Array)); 
                System.out.println("Player 2, your numbers are....." + Arrays.toString(Array2));
            }
            if ((player1 = false) || (player2 = false));
            {

            }
        }
    }               



    private static int randomFill2()
    {
        int randomNum = ThreadLocalRandom.current().nextInt(0, 6 + 1);
        return randomNum;
    }

    private static int randomFill() 
    {
        int randomNum = ThreadLocalRandom.current().nextInt(0, 6 + 1);
        return randomNum;
    }

}

player1 和 player2 都设置为true:

player1 = true;
player2 = true;

执行代码时,while 循环不会识别出两个变量都是true,并且不会显示输出,在这种情况下,是玩家掷出五个骰子的数字

while ((player1 == true) && (player2 == true));
{
    System.out.println("Player 1, your numbers are....." + Arrays.toString(Array)); 
    System.out.println("Player 2, your numbers are....." + Arrays.toString(Array2));
}

我已尝试多次更改它的写入方式,但我无法将while 括号内的任何内容输出

为什么会这样,我该如何解决?

【问题讨论】:

  • while ((player1 == true) &amp;&amp; (player2 == true)); - 删除;
  • 哇非常感谢,原来这么简单
  • 真正的问题是,为什么要在 while 循环中开始?
  • 既然你提到它,我什至没有任何意义。感谢您的观察
  • 为什么所有elses?计算只是if (n &gt; y) /*player 1 goes first*/ else /*player 2 goes first*/

标签: java while-loop


【解决方案1】:

while 循环后有一个分号(请注意,下面的if 语句也会出现同样的问题)。这会导致循环体为空。后面的括号只是define a new scope

例如您的代码被视为:

while ((player1 == true) && (player2 == true)){/*do nothing*/}
//in a new (nested) scope do:
System.out.println("Player 1, your numbers are....." + Arrays.toString(Array)); 
System.out.println("Player 2, your numbers are....." + Arrays.toString(Array2));

不知道为什么你在while 循环中有这些语句,它可能应该是一个if 语句。目前,这将永远打印这些东西,因为里面没有任何东西可以打破循环......

【讨论】:

  • @JAKoharik,如果这个答案解决了你的问题,你可以accept it
猜你喜欢
  • 2015-11-27
  • 2021-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-02
  • 2017-03-06
  • 1970-01-01
相关资源
最近更新 更多