2016-07-11 33 views
0

我需要一些幫助來逐行打印陣列,但它是2個不同的陣列...如何根據我想要的輸出打印我的數組? Row by Row

目前我的代碼允許我打印所有名稱,按照分數,按照平均分數和分數。但是我需要他們根據學生的姓名和輸出得分..看看圖片例如...

現狀:Current

預計輸出..:Expected

public static void main(String[] args) { 

     // 1D Array for Student Names 
     String[] names = new String[10]; 

     //2D Array for test score 
     double[][] scores = new double[10][5]; 

     //1D Array for grades 
     char[] grades = new char[5]; 

     getNames(names); 
     getScores(scores); 

     print1Array(names); 
     print2Array(scores); 

     calculateAvg(scores); 

     classAvg(scores, names); 

    } // PSVMain 

    // Using FOR loop to read and store each name 
    public static void getNames(String[] data) { 
     Scanner kb = new Scanner(System.in); 

     for (int r = 0; r < data.length; r++) { 
      System.out.println("Enter Student names : "); 

      data[r] = kb.next(); 
     } // For R 
    } // getInput 

    // Using FOR loop to read and store each scores 
    public static void getScores(double[][] data) { 
     Scanner kb = new Scanner(System.in); 

     for (int r = 0; r < data.length; r++) { 
      System.out.println("Enter number for row " + (r + 1)); 

      for (int c = 0; c < data[r].length; c++) { 
       System.out.println("Enter number for column " + (c + 1)); 
       data[r][c] = kb.nextInt(); 

      } // For C 
     } // For R 
    } // getInput 

    public static void classAvg(double[][] data, String[] name) { 

     double[] allAverage = new double[data.length]; 

     double totalAvg = 0; 
     double classAvg = 0; 
     double average = 0; 

     for (int r = 0; r < data.length; r++) { 
      double total = 0; 

      for (int c = 0; c < data[r].length; c++) { 
       total = total + data[r][c]; 
      } // For C 

      average = total/data[r].length; 

      // This is the Average for each ROW 
      allAverage[r] = average; 

      totalAvg = totalAvg + allAverage[r]; 

     } // For R 

     classAvg = (totalAvg/name.length); 

     System.out.println("The class Average score is : " + classAvg); 

    } // classAvg 

    // This method will calculate the Average and assigning Grades 
    public static void calculateAvg(double[][] data) { 
     double[] allAverage = new double[data.length]; 
     char[] allGrades = new char[data.length]; 

     double average = 0; 

     for (int r = 0; r < data.length; r++) { 
      double total = 0; 

      for (int c = 0; c < data[r].length; c++) { 
       total = total + data[r][c]; 
      } // For C 

      average = total/data[r].length; 

      // This is the Average for each ROW 
      allAverage[r] = average; 

      if (average >= 85 && average <= 100) { 
       allGrades[r] = 'A'; 
      } else if (average >= 75 && average <= 84) { 
       allGrades[r] = 'B'; 
      } else if (average >= 65 && average <= 74) { 
       allGrades[r] = 'C'; 
      } else if (average >= 50 && average <= 64) { 
       allGrades[r] = 'D'; 
      } else if (average < 50) { 
       allGrades[r] = 'F'; 
      } // If 

      // Print each ROWS average 
      System.out.println("The average for this row is : " + average); 

      // Print each ROWS grades 
      System.out.println("The Grades for this row is : " + allGrades[r]); 

      System.out.println(); 

     } // For R 

    } // Grades 

    // This method allows it to print 1D Array 
    public static void print1Array(String[] data) { 

     for (int r = 0; r < data.length; r++) { 

      System.out.print("The student names are " + data[r] + " "); 

      System.out.println(); 

     } // For R 

    } //print 1D Array 

    // This method allows it to print 2D Array 
    public static void print2Array(double[][] data) { 

     System.out.println(); 
     System.out.println("This are the respective student's score"); 

     for (int r = 0; r < data.length; r++) { 

      for (int c = 0; c < data[r].length; c++) { 

       System.out.print(data[r][c] + " "); 
      } // For C 

      System.out.println(); 
     } // For R 

    } // Print 2D Array 
+3

就像一個sidenode一樣,你可以創建一個'Student'類來存儲每個Student數據,這樣你就不會有多個數組通過他們的索引連接來代表'Student'。在那裏,你可以很容易地實現方法來分開打印學生數據,其實你可以在這裏使用。 – SomeJavaGuy

+0

使用一個循環來打印兩個陣列 –

+0

您的設計使用了所謂的「並行陣列」。這是一種強烈的反模式,即不惜一切代價避免的設計,特別是在面向對象的語言(如Java)中。從20世紀60年代開始,Fortran中除了數組之外沒有數據類型。採取@KevinEsche的建議,並定義一個類來保存學生的所有信息。 –

回答

-1

這個僞代碼可以幫助你實現理想的結果

For i=0 till names.length 
    print names[i] 
    total=0 
    For j=0 till scores[i].length 
     print scores[i][j] 
     total=total+scores[i][j] 
    End For 
    Print Average = total/scores[i].length; 
    decide Grade on basis of average and print 
End For 
+0

對不起,必須downvote,因爲並行陣列是一個壞的反 - 模式。沒有答案應該支持他們的使用。 –

+0

那就好了@JimGarrison。我完全同意你的觀點,並且擁有一個包含所有相關值的學生對象是一個正確的解決方案。我不想給別人一個答案,這就是爲什麼我給了這個備用解決方案。順便提一下,反模式的問題也是不允許的。對? – Sanjeev

+0

我應該根據這個僞代碼創建一個新的方法嗎?我應該在哪裏把這個邏輯放到哪個方法中?對不起,我對數組和Java很陌生。 –