2014-10-30 62 views
0

我是Java學習者並測試Java代碼,目前在Java中並沒有太多掌握。以表格形式顯示兩個一維和一個二維數組

目前我想實現的是我有兩個一維數組的String和一個整數的二維數組。我希望結果以表格形式顯示,但我無法這樣做。我的代碼如下。

import java.util.*; 

public class StudentResults { 
    static String studentName; 
    static String courseName; 
    static int courseMarks; 
    static String[] student = { "Jack Smith", "Jim Lucas", "Beck Barber", 
      "Ann Walker", "Lucy Boxer" }; 

    static String[] course = { "Maths", "Business", "Java", "Design", "Project" }; 

    public static void studentMarks() { 
     int[][] marks = { { 89, 70, 56, 87, 65 }, { 70, 65, 70, 83, 78 }, 
       { 60, 90, 63, 56, 79 }, { 74, 78, 45, 73, 85 }, 
       { 80, 90, 60, 70, 80 } }; 

     for (int i = 0; i < student.length; i++) { 
      studentName = student[i]; 
      System.out.printf("\t\t" + studentName + "\t"); 

     } 
     System.out.println(""); 
     for (int j = 0; j < course.length; j++) { 
      courseName = course[j]; 
      System.out.println(courseName + "\t"); 
     } 
     for (int m = 0; m < marks.length; m++) { 
      for (int n = 0; n < marks.length; n++) { 
       courseMarks = marks[m][n]; 
       System.out.print("\t\t" + courseMarks + "\t\t"); 
      } 
      System.out.println(""); 
     } 

    } 

    public static void main(String args[]) { 
     studentMarks(); 

    } 
} 

我得到的標記輸出是在表中,但不與課程名稱內聯。只是想知道我是否有辦法以我的工作方式實現這一目標。

乾杯,

+0

是System.out.print使用(courseName + 「\ t」 的); – Mohit 2014-10-30 09:47:28

回答

0

我認爲這是某種形式的家庭作業(或自學),所以升將給出提示,而不是完整的解決方案:

要打印的每一個主題,其次是每個標記爲那個特別主題。 當前,您正在打印所有主題的列表,然後您將打印每個主題的標記。

所以,你的這部分代碼:

for (int j = 0; j < course.length; j++) { 
     courseName = course[j]; 
     System.out.println(courseName + "\t"); 
    } 
    for (int m = 0; m < marks.length; m++) { 
     for (int n = 0; n < marks.length; n++) { 
      courseMarks = marks[m][n]; 
      System.out.print("\t\t" + courseMarks + "\t\t"); 
     } 
     System.out.println(""); 
    } 

應該是一個嵌套的循環,而不是兩個單獨的迴路。即第二個循環應該在第一個循環內部。

所以你打印一個主題,那麼所有對這個問題,那麼接下來的問題,那麼所有對於第二個主題的標記等

還有一點擺弄周圍,然後將得到的標記你想要的確切佈局。但請記住,System.out.println()將打印出一個新行後面的參數。

+0

System.out.print(courseName +「\ t」);將是一個很好的方式來顯示記錄:) – Mohit 2014-10-30 09:46:27

+0

謝謝cowls的答覆。我已經試圖通過在我的m循環後面加上} for j循環來實現它的嵌套循環,但是它給了我一個顯示,就像每行的一個新表一樣。 (我不想要的)我只想第一行的結果拿出數學,第二行的商標等等...... – Newbie 2014-10-30 09:48:04

+0

你可以在第二個循環中刪除外部for循環:'for(int m = 0; m cowls 2014-10-30 09:49:43

0

刪除用於打印課程的第二個for循環,並在第三個循環中添加打印課程的邏輯。

代碼應該看起來像這樣。

import java.util.*; 

public class StudentResults { 
static String studentName; 
static String courseName; 
static int courseMarks; 
static String[] student = { "Jack Smith", "Jim Lucas", "Beck Barber", 
     "Ann Walker", "Lucy Boxer" }; 

static String[] course = { "Maths", "Business", "Java", "Design", "Project" }; 

public static void studentMarks() { 
    int[][] marks = { { 89, 70, 56, 87, 65 }, { 70, 65, 70, 83, 78 }, 
      { 60, 90, 63, 56, 79 }, { 74, 78, 45, 73, 85 }, 
      { 80, 90, 60, 70, 80 } }; 

    for (int i = 0; i < student.length; i++) { 
     studentName = student[i]; 
     System.out.printf("\t\t" + studentName + "\t"); 

    } 
    System.out.println(""); 
    /*for (int j = 0; j < course.length; j++) { 
     courseName = course[j]; 
     System.out.println(courseName + "\t"); 
    }*/ 
    for (int m = 0; m < marks.length; m++) { 
     courseName = course[m]; 
     System.out.print(courseName + "\t\t"); 
     for (int n = 0; n < marks.length; n++) { 
      courseMarks = marks[m][n]; 
      System.out.print("\t\t" + courseMarks + "\t\t"); 
     } 
     System.out.println(""); 
    } 

} 

public static void main(String args[]) { 
    studentMarks(); 

} 

}

+0

你們搖滾。 非常感謝您的幫助,解決了問題... – Newbie 2014-10-30 09:55:30