【问题标题】:Creating a specific 2D array of random Boolean values创建由随机布尔值组成的特定二维数组
【发布时间】:2020-01-04 04:17:14
【问题描述】:

我目前正在编写一个程序,该程序将在给定大小的情况下创建一个二维布尔矩阵。我需要在随机对象中使用种子来创建组成网格的真/假值。假值应该用 - 表示,真值用 # 表示。 我目前坚持使用种子来制作真/假值网格,我不确定如何用指定的字符表示真/假值。

这是我当前的代码。

import java.util.Arrays;
import java.util.Random;

public class Life {

public static void main(String [] args) {
    int rows = 6;
    int columns = 8;
    Random rand = new Random();
    rand.setSeed(7);
    boolean[][]matrix = new boolean[rows][columns];

    for (int row = 0; row < matrix.length; row++ ) {
        for (int col = 0; col < matrix[row].length; col++) {

            matrix[row][col] = rand.nextBoolean();

            System.out.print(matrix[row][col] + "\t");

        }
    }
}
}

我目前的结果如下:

true    true    true    false   false   false   true    true    true    true    false   true    false   false   true    true    false   true    true    false   true    false   true    true    true    true    false   true    false   true    false   true    true    true    false   true    false   true    true    true    false   true    false   false   true    true    false   true

这都是一条直线,而不是 6 x 8 的网格。

我想说的是这样的

- = - - = - = -
= - - = - = - -
- - = - - = - = 
- = - - = - = -
= - = - - = - -
- - - = = - - =

【问题讨论】:

    标签: java arrays for-loop boolean


    【解决方案1】:

    首先,您需要根据数组中的值来决定要打印哪个字符。这可以通过三元运算符来完成:

    System.out.print((matrix[row][col] ? "=" : "-") + " "); // a space is fine, you don't need a tab.
    

    然后,您需要在外部 for 循环的每次迭代结束时打印额外的一行,因为外部 for 循环是打印行的循环。

    您的循环应如下所示:

    for (int row = 0; row < matrix.length; row++ ) {
        for (int col = 0; col < matrix[row].length; col++) {
    
            matrix[row][col] = rand.nextBoolean();
    
            System.out.print((matrix[row][col] ? "=" : "-") + " ");
    
        }
        System.out.println();
    }
    

    【讨论】:

    • 感谢分享您的编辑。我忘记了我实际上需要一个特别默认布尔值的边框,所以“-”。我是否需要另一个嵌套的 for 循环,或者我会更改现有代码?
    • 我不明白您所说的“特定默认布尔值的边框”是什么意思。您可以编辑您的问题以显示您的意思吗? @LukeCagno
    【解决方案2】:

    查看@Sweeper 的答案以获得更简单的解决方案。

    您也可以使用流来做到这一点:

    String matrix = IntStream.range(rows)
        .mapToObj(i -> IntStream.range(columns)
            .mapToObj(j -> rand.nextBoolean())
            .map(b -> b? "=" : "-")
            .collect(Collectors.joining("\t")))
        .collect(Collectors.joining("\n")))
    

    【讨论】:

      【解决方案3】:

      我会使用enum 来表示单元格,您可以使用两个可能的值来创建它,分别代表您的“TRUE”和“FALSE”状态,一个将boolean 映射到正确值并覆盖的函数toString() 便于打印。喜欢,

      static enum Cell {
          FALSE, TRUE;
          public static Cell fromBoolean(boolean a) {
              if (a) {
                  return TRUE;
              }
              return FALSE;
          }
      
          @Override
          public String toString() {
              if (this == TRUE) {
                  return "=";
              }
              return "-";
          }
      }
      

      现在您的main 方法可以将布尔值从matrix 转换为Cellprint 之类的;

      for (int row = 0; row < matrix.length; row++) {
          for (int col = 0; col < matrix[row].length; col++) {
              if (col != 0) {
                  System.out.print(' ');
              }
              matrix[row][col] = rand.nextBoolean();
              System.out.print(Cell.fromBoolean(matrix[row][col]));
          }
          System.out.println();
      }
      

      【讨论】:

        猜你喜欢
        • 2015-03-26
        • 2016-08-03
        • 2015-04-17
        • 1970-01-01
        • 2016-05-05
        • 2021-06-30
        • 1970-01-01
        • 2018-08-22
        • 2021-02-04
        相关资源
        最近更新 更多