使用System.Out.Print在DrJava中的同一行上打印

时间:2015-06-25 05:42:30

标签: java drjava

我正在尝试使用DrJava IDE打印网格。但是我的输出显示在一行,尽管我在内部循环中使用了System.out.Print()。为什么会这样?

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有关吗?

1 个答案:

答案 0 :(得分: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(" ");