2013-11-04 82 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