【问题标题】:Printing on the same line in DrJava using System.Out.Print使用 System.Out.Print 在 DrJava 中的同一行上打印
【发布时间】:2015-06-25 05:42:30
【问题描述】:

我正在尝试使用 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 有关?

【问题讨论】:

  • System.out.print 打印在同一行,System.out.println 打印并在末尾添加一个“换行符”。也就是说 - 您的输出看起来像 确实 在单独的行中打印...
  • 我希望它在每行打印 10 个值。这就是我认为我的代码要产生的内容。
  • 内循环只有 S.o.print(),外循环有 S.o.println()。
  • 不,两者都在内部循环中-在下面的答案中查看更多详细信息

标签: java drjava


【解决方案1】:

你有几个额外的花括号让你感到困惑:

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(" ");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-19
    • 1970-01-01
    • 1970-01-01
    • 2021-07-24
    • 2011-08-01
    • 2014-01-29
    相关资源
    最近更新 更多