使用方法分析1和0的表

时间:2013-10-27 18:26:29

标签: java arrays methods

我无法让程序正确编译。我试图创建一个1和0的随机表。它们的大小由用户决定,程序应该分析上面的表并打印另一个表,计算连续1的数量。

import java.util.Scanner;

public class Homework {

 public static void scanInfo()
 {

 Scanner input = new Scanner(System.in);

System.out.println("Enter number of rows: ");    
int rows = input.nextInt();

System.out.println("Enter number of columns: "); 
int columns = input.nextInt();

int[][] nums = new int[rows][columns];            

 static int[][] sample = nums;

}
   static int[][] results = new int[4][5];
   static int goodData = 1;

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

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

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

   static int analyzeCell(int row, int col) {
      int xCol = col;   // varies in the loop
      int runCount = 0;  // initialize counter
      int rowLen = sample[row].length;   // shorthand
      int hereData = sample[row][xCol];  // shorthand
      while (hereData == goodData && xCol < rowLen) {
         runCount++;
         xCol++;
         if (xCol < rowLen) { hereData = sample[row][xCol];} 
      }
      return runCount;
   }

  /*   Start Print Stuff */
  public static void printTable(int[][] aTable ) {
    for (int[] row : aTable) {
      printRow(row);
      System.out.println();
    }
  }
  public static void printRow(int[] aRow) {
    for (int cell  : aRow) {
      System.out.printf("%d ", cell);
    }
  }
}
  /*   End Print Stuff */

1 个答案:

答案 0 :(得分:0)

有一大堆错误,但这应该让你开始。

在任何方法之外,放

static int[][] nums = null;            
static int[][] sample = null;

然后在scanInfo内,将行更改为

nums = new int[rows][columns];
sample = nums;

另外,请从scanInfo()致电main()