使用方法的1和0的随机数组

时间:2013-10-26 22:27:13

标签: java methods random

我正在尝试创建一个1和0的随机大小的数组。如果我删除它的随机方面并手动输入数组的大小,我可以让程序运行和编译。出于某种原因,当我引入随机实用程序时,我无法编译程序。

mport java.util.Random;

public class Project1a {

    int[][] sample={{0,0,1,1,1},
                          {1,1,0,1,1},
                          {1,1,1,0,1},
                          {0,1,0,1,1}};

     int box[][];

    Random randomNumbers = new Random();


     int m = randomNumbers.nextInt(100);
     int n = randomNumbers.nextInt(100);


     int results[][] = new int [m][n];
     int goodData = 1;

   public  static void main(String[] args){
      analyzeTable();
      printTable(results);
   }

   public void analyzeTable() {
      int row=0;
      while (row < sample.length) {
         analyzeRow(row);
         row++;
      }
   }

   public void analyzeRow(int row) {
      int xCol = 0;
      int rCount = 0;
      while (xCol < sample[row].length) {
         rCount = analyzeCell(row,xCol);
         results[row][xCol] = rCount; 
         xCol++;
      }
   }

    int analyzeCell(int row, int col) {
      int xCol = col;   
      int runCount = 0;  
      int rowLen = sample[row].length;   
      int hereData = sample[row][xCol]; 
      while (hereData ==  goodData && xCol < rowLen) {
         runCount++;
         xCol++;
         if (xCol < rowLen) { hereData = sample[row][xCol];} 
      }
      return runCount;
   }

  public  void printTable(int[][] aTable ) {
    for (int[] row : aTable) {
      printRow(row);
      System.out.println();
    }
  }
  public void printRow(int[] aRow) {
    for (int cell  : aRow) {
      System.out.printf("%d ", cell);
    }
  }

}

2 个答案:

答案 0 :(得分:2)

您的班级名称与java.util.Random冲突。重命名你的课程是最简单的解决方法。

答案 1 :(得分:0)

问题是你在一个块中声明了你的实例变量,使其在方法

中无法访问

选中此处:从此处删除大括号{} 以及您访问resultsgoodData的方式,您需要声明static

    { // remove this
Random randomNumbers = new Random();


int m = randomNumbers.nextInt(100);
int n = randomNumbers.nextInt(100);


 static int results = new int [m][n];   
 static int goodData = 1;
} // remove this