用java打印棋盘的网格?
我试图打印一个棋盘格,但它只是打印一个文件,然后再打印出7个空行 这是我的密码:用java打印棋盘的网格?,java,Java,我试图打印一个棋盘格,但它只是打印一个文件,然后再打印出7个空行 这是我的密码: public class Board { public static void main(String[] args) { char rows = 'a'; int col = 0; String spot; int[][] grid = new int [8][8]; for(int i = 0; i <=
public class Board
{
public static void main(String[] args)
{
char rows = 'a';
int col = 0;
String spot;
int[][] grid = new int [8][8];
for(int i = 0; i <= grid.length; i++, rows++)
{
for(; col < grid.length; col++)
{
System.out.print(rows + "" + (col + 1) + " ");
}
System.out.println();
}
}
}
公共课程委员会
{
公共静态void main(字符串[]args)
{
字符行='a';
int col=0;
弦斑;
int[][]网格=新int[8][8];
for(int i=0;i使用增强的for循环:
for(int[] row : grid){
for(int square : row){
System.out.print(square + " ");
}
System.out.println();
}
使用增强的for循环执行此操作:
for(int[] row : grid){
for(int square : row){
System.out.print(square + " ");
}
System.out.println();
}
您必须重置col变量。否则它将不会执行第二个for循环,因为条件get failed col
public static void main(String[] args)
{
char rows = 'a';
int col = 0;
String spot;
int[][] grid = new int [8][8];
for(int i = 0; i <= grid.length; i++, rows++)
{
for(col=0; col < grid.length; col++)
{
System.out.print(rows + "" + (col + 1) + " ");
}
System.out.println();
}
}
publicstaticvoidmain(字符串[]args)
{
字符行='a';
int col=0;
弦斑;
int[][]网格=新int[8][8];
对于(int i=0;i您必须重置col变量。否则它将不会执行第二个for循环,因为条件get failed col
public static void main(String[] args)
{
char rows = 'a';
int col = 0;
String spot;
int[][] grid = new int [8][8];
for(int i = 0; i <= grid.length; i++, rows++)
{
for(col=0; col < grid.length; col++)
{
System.out.print(rows + "" + (col + 1) + " ");
}
System.out.println();
}
}
publicstaticvoidmain(字符串[]args)
{
字符行='a';
int col=0;
弦斑;
int[][]网格=新int[8][8];
对于(int i=0;i,应正确初始化col
变量
For循环条件应该是适当的
请参阅以下代码:
public class Board
{
public static void main(String[] args)
{
char rows = 'a';
String spot;
int[][] grid = new int [8][8];
for(int i = 0; i < grid.length; i++, rows++)
{
for(int col = 0; col < grid[i].length; col++)
{
System.out.print(rows + "" + (col + 1) + " ");
}
System.out.println();
}
}
}
应正确初始化col
变量
For循环条件应该是适当的
请参阅以下代码:
public class Board
{
public static void main(String[] args)
{
char rows = 'a';
String spot;
int[][] grid = new int [8][8];
for(int i = 0; i < grid.length; i++, rows++)
{
for(int col = 0; col < grid[i].length; col++)
{
System.out.print(rows + "" + (col + 1) + " ");
}
System.out.println();
}
}
}
您知道如何读取数组,对吗?您必须访问这样的元素grid[1][2]
。您以相同的方式打印它。我没有看到任何尝试在打印语句中使用grid
,您需要在方括号内提供索引[]。查看我的答案。我指出了问题并发布了更正的代码。你知道如何读取数组,对吗?你必须访问这样的元素grid[1][2]
。你以相同的方式打印它。我没有看到任何人试图在打印语句中使用grid
,你需要在方括号内提供索引[].见我的答案。我指出了问题并发布了更正后的代码。