2013-11-04 169 views
1
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上的值,等等等等为什么我的数组不能在2d中打印?

但其输出它全部在同一行中,如:

0.0 0.0 0.0 0.0

而非

0.0 0.0(行A)

0.0 0.0(B行)

回答

3

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

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! 
} 
1

也许,最内层的for循环之后添加与

System.out.println(""); 

断行?

1

你的第一个循环的末尾添加

System.out.println(""); 

+0

哈,非常感谢。多么愚蠢的错误。 – NickP

+0

@NickP很高兴帮助。请记住,你可以提出一个你认为有帮助的答案,或者甚至接受那个你认为解决了你的问题的答案。 –

+0

我很乐意回复一个答案,但它需要15个声望点。 – NickP