java - 为什么我的阵列不是二维打印?

标签 java arrays

  rowsTotal = new double[rows];
  positions = new double [rows][];

  for(index = 0; index < rows; index++){
      System.out.print("Please enter number of positions in row " + (char)(index+65) + " : ");
      pos = keyboard.nextInt();
      if(pos > MAX_POSITIONS){
          System.out.print("ERROR: Out of range, try again: ");
          pos = keyboard.nextInt();
      }
      positions[index] = new double[pos];


      }


  System.out.print("(A)dd, (R)emove, (P)rint,          e(X)it : ");
  input = keyboard.next().charAt(0);
  input = Character.toUpperCase(input);


  if(input == 'P'){
      for(int index1= 0; index1 < rows; index1++){
          for(int pos1= 0; pos1 < pos; pos1++){

              System.out.print(positions[index1][pos1] + " "); 
          }



  }

  }

我希望输出将其显示为每一行的矩阵,因此对于第一行,它将是 A 行的值,第二行将是 B 行的值,等等

但它在同一行中输出所有内容,例如:

0.0 0.0 0.0 0.0

而不是

0.0 0.0(A 行)

0.0 0.0(B 行)

最佳答案

你忘记了每行后的换行符。

for(int index1 = 0; index1 < rows; index1++)
{
    for(int pos1 = 0; pos1 < pos; pos1++)
    {
        System.out.print(positions[index1][pos1] + " ");
    }
    System.out.println(); // <-- This one!
}

关于java - 为什么我的阵列不是二维打印?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19759914/

相关文章:

java - 如何从 Activity 返回到上一个 fragment

java - Eclipse - GWT - Java - MySQL - 如何正确捕获异常

javascript - 如何向现有对象添加原型(prototype)属性

arrays - Bash 数组保存

java - 错误: ORA-01008: not all variables bound call SQL function

java - 阅读我自己的 Jar list 2

arrays - .splice(x,1) 不起作用

arrays - 如何在 swift 中简化数组枚举

c++ - 在构造函数的初始化列表中初始化数组

java - 单例操作与多实例操作