2015-06-25 19 views
2

我試圖使用DrJava IDE打印網格。然而,我的輸出顯示在一行中,儘管我在我的內循環中使用了System.out.Print()。這是爲什麼發生?使用System.Out.Print在DrJava的同一行上打印

public class Percolation 
{ 
    private boolean[][] grid; 
    private int N = 0; 
    private boolean[] conv; 

    public Percolation(int N)    // create N-by-N grid, with all sites blocked 
    { 
     this.N = N; 
     checkNegative(N); 
     grid = new boolean[N+1][N+1]; 
    } 

    private void checkNegative(int N) 
    { 
     if(N <= 0) 
      throw new java.lang.IllegalArgumentException("Number of sites less than 1"); 
    } 
    private void checkBounds(int i, int j) 
    { 
     if((i > N)||(j > N)) 
     { 
      throw new java.lang.IndexOutOfBoundsException("Index out of bounds- " + i + "," + j + " out of bounds"); 
     } 
     else 
      return; 
    } 

    private boolean[] convertTo1D(boolean g[][]) 
    { 
     int i,j; int k = 0; 
     boolean[] conv = new boolean[N*N]; 
     for(i = 1; i <= N; i++) 
      for(j =1; j <= N; j++) 
      { 
       conv[k] = g[i][j]; 
      } 

     return conv; 
    } 

    private void displayGrid() 
    { 
     int i,j; 
     for(i = 1; i < N+1; i++) 
      for(j =1; j < N+1; j++) 
      { 
       { 

        System.out.print(i + " " + j); 

       } 
       System.out.println(" "); 
      } 

    } 

    public void open(int i, int j)   // open site (row i, column j) if it is not open already 
    { 

     checkBounds(i,j); 

     if (isOpen(i,j) == true) 
      return; 
     else 
      grid[i][j] = true; 
    } 

    public boolean isOpen(int i, int j)  // is site (row i, column j) open? 
    { 
     checkBounds(i,j); 

     return grid[i][j]; 
    } 

    public boolean isFull(int i, int j)  // is site (row i, column j) full? 
    { 
     checkBounds(i,j); 

     return grid[i][j]; 
    } 

    public boolean percolates()    // does the system percolate? 
    { 
     return false; 
    } 


    public static void main(String[] args) // test client (optional) 
    { 
     int K; 
     System.out.println("Enter grid length"); 
     Scanner in = new Scanner(System.in); 
     K = in.nextInt(); 

     Percolation Perc = new Percolation(K); 

     WeightedQuickUnionUF join = new WeightedQuickUnionUF(K); 

     Perc.displayGrid(); 


    } 

} 

輸出 -

1 1 
1 2 
1 3 
1 4 
1 5 
1 6 
1 7 
1 8 
1 9 
1 10 
2 1 
2 2 
2 3 
2 4 
2 5 
2 6 
2 7 
2 8 
2 9 
2 10 
3 1 
3 2 
3 3 
3 4 
3 5 
3 6 
3 7 
3 8 
3 9 
3 10 
4 1 
4 2 
4 3 
4 4 
4 5 
4 6 
4 7 
4 8 
4 9 
4 10 
5 1 
5 2 
5 3 
5 4 
5 5 
5 6 
5 7 
5 8 
5 9 
5 10 
6 1 
6 2 
6 3 
6 4 
6 5 
6 6 
6 7 
6 8 
6 9 
6 10 
7 1 
7 2 
7 3 
7 4 
7 5 
7 6 
7 7 
7 8 
7 9 
7 10 
8 1 
8 2 
8 3 
8 4 
8 5 
8 6 
8 7 
8 8 
8 9 
8 10 
9 1 
9 2 
9 3 
9 4 
9 5 
9 6 
9 7 
9 8 
9 9 
9 10 
10 1 
10 2 
10 3 
10 4 
10 5 
10 6 
10 7 
10 8 
10 9 
10 10 

有什麼錯我的代碼還是有一些做的IDE?

+0

'在同一行System.out.print'版畫, 'System.out.println'打印並在最後添加一個「換行符」。這就是說 - 你的輸出看起來像*不會*在單獨的行中打印...... – alfasin

+0

我希望它在每行中打印10個值。這是我認爲我的代碼是要生產的。 –

+0

我只在內部循環中使用S.o.print(),在外部循環中使用S.o.println()。 –

回答

0

你有幾個額外的大括號,混淆你:

for(j = 1; j < N+1; j++) 
{ 
    { 

     System.out.print(i + " " + j); 

    } 
    System.out.println(" "); 
} 

你可能想要做的是:

for(j = 1; j < N+1; j++) 
{ 
    System.out.print(i + " " + j);  
} 
System.out.println(" "); 
+0

哦,我的上帝。謝謝。 –