【问题标题】:How to set up this 2D Boolean Array?如何设置这个二维布尔数组?
【发布时间】:2017-03-22 20:48:44
【问题描述】:

所以我必须解决这个问题,但是这些数组让我感到困惑,因为我不知道如何将布尔值与数组结合起来,然后正确地组织它们。我知道这是一个完全错误的代码,但我真的不知道该怎么做。这些是说明:

声明一个大小为 5 和 10 的 2D 布尔数组

如果随机值为 0.5,则用 true 填充数组,否则为 false

打印数组中false的个数

每行打印一行

每行打印数组索引

只打印具有真值的单元格的索引

  public class DoubleArray {

  public static void main(String [] args) {


  double [][] a = new double[10][15];
  int zeroCount = 0;               
  for (int i= 0; i<a.length;i++)
   {                               
      if (Math.random()>0.5)  {

           System.out.println("1" + "\t" + "1");
       }
    else 
    {

        zeroCount++;    // increment
        System.out.println("0" + "\t" + "0");

    }
}
System.out.println("Number of zeros is " + zeroCount);  

}
}

【问题讨论】:

  • 不幸的是,您的程序与您被要求做的事情相去甚远。一些提示:为什么要声明一个double 的二维数组,而它应该是boolean?为什么尺寸与规格不同?要使用 2D 数组,您通常需要嵌套循环。
  • 如果我的回答确实帮助您取得了进步(例如接受它),请告诉我;或者让我知道您是否需要更多帮助。

标签: java arrays if-statement boolean


【解决方案1】:

非常简单:您只需迭代该数组并将值分配给每个单元格:

boolean booleanData[][] = new boolean[10][5];
for(int i = 0; i < booleanData.length; i++) {
  for(int j = 0; j < booleanData[i].length; j++) {
    if ( if (Math.random()>0.5) {
      booleanData[i][j] = true;
    } else {
      booleanData[i][j] = false;
      zeroCount++;
    }

您使用相同类型的循环来打印数组元素,或对其内容进行其他检查。

或者,使用更简单的 for each 循环:

for (boolean[] row : booleanData) {
  for (boolean aBool : row) {
     ... for printing, unfortunately not for assigning values ...

【讨论】:

    【解决方案2】:

    虽然 GhostCat 已经有了很好的答案,但我还是决定只是为了好玩而制作一个基本版本的您所要求的内容。这是我想出的:

            boolean[][] a = new boolean[5][10];
            int falseCount = 0;
            String trueIndexes = "";
    
            for (int i = 0; i < a.length; i++){         //For every row...
                for (int j = 0; j < a[i].length; j++){  //For every column...
    
                     if (Math.random() > 0.5)  {        //Assign values to array
                         a[i][j] = true;
                     }
                     else{
                         a[i][j] = false;
                         falseCount++;                  //For every false value, increase the count
                     }
                 }
            }
            System.out.println(falseCount);             //Print number of false values
    
            for (int i = 0; i < a.length; i++){
                for (int j = 0; j < a[i].length; j++){
    
                     System.out.print(a[i][j] + " ");   //Print out values
    
                }
                System.out.println();
            }
    
    
            for (int i = 0; i < a.length; i++){
                for (int j = 0; j < a[i].length; j++){
    
                     System.out.print(j + " ");         //Print out indexes
                     if(a[i][j] == true){
                         trueIndexes += " " + i + "," + j + " "; //Add up true value indexes
                     }
                }
                System.out.println();
            }
    
            System.out.println(trueIndexes);            //Print out true values
    

    打印出来:

    23
     false false false false true true false false false false 
     true false false true true true true false true false 
     true true false true false false true true false true 
     false false false true true true true false true true 
     true true false true false true true true true false 
     0 1 2 3 4 5 6 7 8 9 
     0 1 2 3 4 5 6 7 8 9 
     0 1 2 3 4 5 6 7 8 9 
     0 1 2 3 4 5 6 7 8 9 
     0 1 2 3 4 5 6 7 8 9 
    0,4  0,5  1,0  1,3  1,4  1,5  1,6  1,8  2,0  2,1  2,3  2,6  2,7  2,9  3,3  3,4  3,5  3,6  3,8  3,9  4,0  4,1  4,3  4,5  4,6  4,7  4,8
    

    您可以进行许多改进,例如组合用于打印元素的两个for 循环,(提示:看看trueIndexes 是如何实现的,并记住"\n")但我希望这个例子能给出你有一些上下文来帮助你制作你自己的版本。

    【讨论】:

      猜你喜欢
      • 2017-01-26
      • 2022-01-23
      • 2014-03-08
      • 2015-04-17
      • 2021-11-13
      • 2020-04-28
      • 2016-01-22
      • 2021-02-04
      • 2018-02-10
      相关资源
      最近更新 更多