-5

我无法让程序正确编译。我试图创建一个随机的 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 */
4

1 回答 1

0

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

在任何方法之外,把

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

然后在里面scanInfo,把线改成

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

另外,scanInfo()main().

于 2013-10-27T18:31:19.333 回答