2013-10-28 4 views
0

프로그램이 준수하지만 인쇄 할 결과 테이블을 가져올 수 없습니다. 이 프로그램은 1과 0의 무작위 표를 분석하여 번호가 1 인 순서대로 표를 인쇄합니다. 테이블 크기는 사용자가 작성한 입력에 의해 생성됩니다. 그건 제대로 작동하지만 인쇄 결과 테이블을 얻을 수 없습니다. 임의의 유틸리티가 작동하는 다른 유형에 감사했습니다. 프로그램이 컴파일되지만 테이블이 인쇄되지 않습니다

지금 난 그냥

import java.util.Scanner; 
import java.util.Random; 

public class Project1a { 
    static int[][] results; 
    static int[][] sample; 

    static int goodData = 1; 

    public static void main(String[] args) { // main comes first (or last) 
     scanInfo(); 
     analyzeTable(); 
     printTable(results); 

    } 


    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(); 
     Random randomNumbers = new Random(); 
     sample = new int[rows= randomNumbers.nextInt(50)][columns = randomNumbers.nextInt(50)]; 
     results = new int[rows][columns]; 


    } 



    static void analyzeTable() { // no argument. static var sample is assumed 
     int row=0; 
     while (row < sample.length) { 
      analyzeRow(row); 
      row++; 
     } 
    } 
    static void analyzeRow(int row) { // assume sample is "global" 
     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; 
     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 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); 
    } 
    } 
} 

답변

1

귀하의 문제가이 라인입니다 .... 제로의 테이블이 가득 얻을. 당신이 제로에 rows을 설정하는, 그래서

sample = new int[rows= randomNumbers.nextInt(1)][columns = randomNumbers.nextInt(2)]; 

당신은 참조 nextInt(1)는 항상 0 반환, 당신은 전혀 행 배열의 부부와 함께 끝. nextInt를위한 JavaDoc에서

-

public int nextInt(int n)

이 난수 생성기의에서 그려진 의사, 균일 0 (포함) 사이 분산 int 값과 지정된 값 (전용)을 반환 순서.

관련 문제