使用 System.Out.Print 在 DrJava 的同一行上打印
Printing on the same line in DrJava using System.Out.Print
我正在尝试使用 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有关?
你有几个额外的花括号让你感到困惑:
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(" ");
我正在尝试使用 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有关?
你有几个额外的花括号让你感到困惑:
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(" ");