【问题标题】:Random boolean 2d-array in Java returning always "false"Java中的随机布尔二维数组总是返回“假”
【发布时间】:2016-05-05 22:47:47
【问题描述】:

我只是想创建一个随机布尔二维数组,但它总是返回 false...运算符“&&”有问题吗?没看懂……

public static void main(String[] args){

    boolean[][] arr = new boolean[5][5];
    Random r = new Random();
    boolean row = r.nextBoolean();
    boolean col = r.nextBoolean();

    for(int i=0 ; i<arr.length ; i++){

        for(int j=0;j<arr[i].length;j++){

        arr[i][j] = row && col;

        System.out.print(arr[i][j]+"\t");

    }

}

【问题讨论】:

    标签: java arrays boolean


    【解决方案1】:

    你只计算 row 和 col 一次,试试这个:

    boolean[][] arr = new boolean[5][5];
    Random r = new Random();
    boolean row;
    boolean col;
    
    for (int i = 0; i < arr.length; i++) {
        for (int j = 0; j < arr[i].length; j++) {
            row = r.nextBoolean();
            col = r.nextBoolean();
            arr[i][j] = row && col;
            System.out.print(arr[i][j] + "\t");
        }
    }
    

    【讨论】:

    • 我不确定将rowcol 分开,然后取其中的&amp;&amp; 是否有任何意义。它所做的只是将true 元素的数量减少了大约两倍。
    • @khelwood 对,两个,不是四个!
    • 我想也许他以后想从中得到一些东西,否则他为什么要有两个随机数?啊,我想我现在知道了,他到底想要什么。但这应该是另一个答案。
    • 很难说 OP 想要做什么。我唯一的猜测是他可能希望在每一行和每一列中都有相同的booleans,例如boolean[] row = new boolean[5]; boolean[] col = new boolean[5]; 将它们设置为随机值,然后使用 arr[i][j] = row[i] &amp;&amp; col[j]
    • 这只是我的想法,因此是第二个答案
    【解决方案2】:

    我认为你想要的只是为数组中的每个单元格创建一个新的随机布尔值,如下所示:

    public static void main(String[] args){
    
        boolean[][] arr = new boolean[5][5];
        Random r = new Random();
    
        for(int i = 0; i < arr.length; i++){
            for(int j = 0; j < arr[i].length; j++){
    
                arr[i][j] = r.nextBoolean();
    
                System.out.print(arr[i][j]+"\t");
            }
        }   
    }
    

    【讨论】:

    • 乐于助人!你能把我的答案标记为正确吗?
    【解决方案3】:

    在考虑了您可能想要什么之后,这似乎更合理: 每行一个布尔值,每列一个布尔值,给出:

    int columns =5;
    int rows =5;
    boolean[][] arr = new boolean[rows][columns];
    Random r = new Random();
    boolean[] row = new boolean[rows];
    boolean[] col = new boolean[columns];
    
    for(int i=0; i<rows; i++){
        row[i] = r.nextBoolean();
    }
    for(int i=0; i<columns; i++){
        col[i] = r.nextBoolean();
    }
    
    for (int i = 0; i < arr.length; i++) {
        for (int j = 0; j < arr[i].length; j++) {
            arr[i][j] = row[i] && col[j];
            System.out.print(arr[i][j] + "\t");
        }
        System.out.println();
    }
    

    【讨论】:

      【解决方案4】:
      Here is compiled and working JAVA solution
      
      
      /* package whatever; // don't place package name! */
      
      import java.util.*;
      import java.lang.*;
      import java.io.*;
      
      /* Name of the class has to be "Main" only if the class is public. */
          public static void main (String[] args) throws java.lang.Exception
          {
              boolean[][] arr = new boolean[5][5];
              int cnt = 0;
      
              for(int i = 0; i < arr[i].length; i++)
              {
                  for(int j = 0; j < arr[i].length; j++)
                  {
                      Random r = new Random();
                      boolean row = r.nextBoolean();
                      boolean col = r.nextBoolean();
                      arr[i][j] = row && col;
                      System.out.print(arr[i][j]+"\t");
                  }
              }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-28
        • 2020-01-04
        • 2022-01-01
        • 2017-04-17
        • 1970-01-01
        相关资源
        最近更新 更多