-1

这是我的打印正方形方法

public void printSquare()
{
  DecimalFormat newSquare = new DecimalFormat("00");
  for (int row = 0; row < square.length; row++)
  {
     for (int col = 0; col < square[row].length; col++)
     {  
        System.out.print((newSquare.format(square[row][col])) + " ");
     }
  }
System.out.println();
System.out.println();

}

这就是它的输出的样子

08 01 06 03 05 07 04 09 02

这就是我需要它的样子

08 01 06

03 05 07

04 09 02

我一直在努力解决这个问题,任何帮助将不胜感激!谢谢!

4

2 回答 2

1

您必须在每行之后添加换行符

public void printSquare()
{
  DecimalFormat newSquare = new DecimalFormat("00");
  for (int row = 0; row < square.length; row++)
  {
     for (int col = 0; col < square[row].length; col++)
     {  
        System.out.print((newSquare.format(square[row][col])) + " ");
     }
     System.out.println(); // this will print new line after each row
  }
}
于 2015-02-04T15:59:45.717 回答
1

它将通过System.out.println("\n")在外循环的末尾使用 a 来修复

public void printSquare()
{
  DecimalFormat newSquare = new DecimalFormat("00");
  for (int row = 0; row < square.length; row++)
  {
     for (int col = 0; col < square[row].length; col++)
     {  
        System.out.print((newSquare.format(square[row][col])) + " ");
     }
     System.out.println("\n");
  }
System.out.println();
System.out.println();
}
于 2015-02-04T16:00:20.303 回答