2016-03-02 72 views
1

我是Java新手(一般編碼),所以我很抱歉提前發現錯誤。我相信可能有更簡單的方法來做到這一點,但我無法弄清楚。 我覺得我也犯了一個非常明顯的錯誤。並排打印一維和二維數組

這是我一直在問的事:

編寫一個程序,以隨機順序和 他/她相應的3分項目的成績將輸入的10學生姓名。這些數據將被讀取爲 爲2個單獨的數組:一個字符串數組以保存名稱,以及一個數字的二維數組來保存分數。您可以從鍵盤或文本文件輸入 數據。

以表格形式輸出輸入數據。

使用選擇排序,按字母順序重新排列隨機學生姓名 。按照排序順序,相應的分數顯然需要 。 以 表格形式輸出排序後的名稱和分數。

使用二進制搜索,您的程序將允許用戶輸入名稱。 您的程序將輸出名稱及其對應的3個項目 分數。

對於3個項目中的每一個,輸出 得分最高的學生姓名。

粗體部分是卡住的地方。我編譯並正常運行,但是在打印表格時,例如只輸入最後一組輸入的3個數字(是的,格式化需要進行處理,以便它是一個表格,這只是一個臨時格式直到我解決了這個問題) This is what I get.其餘的名字被切斷了,但它們都打印了相同的分數。

import java.util.Scanner; 

/**Write a program that will input 10 students names 
* in random order and their corresponding 3 project scores. 
* Data will be read into 2 separate arrays: an array of strings 
* for the names and a 2D array for the scores. 
* Output input data in table form 
*For each of the 3 projects, output the name of the student(s) who scored 
the highest mark. 
**/ 

public class NameAndGrades 
{ 
public static void main (String[] args) 
{ 
    //Scanner object to allow user input 
    Scanner scnr = new Scanner(System.in); 

    //This array will store the names of the students 
    String[] names = new String[10]; 

    //This 2D array will store the 3 project scores 
    int[][] scores = new int [10][3]; 


     //Ask user to input the student names 
     System.out.println("Enter the 10 student names: "); 

     for (int index = 0; index < 10; index++) 
     { 
     System.out.print("Student " + (index +1) + ": "); 
     names[index] = scnr.nextLine(); 
     } 


     selectionSort(names); 


     //Will print the names alphabetically 
     //THIS WILL BE DELETED LATER, IT WAS JUST TO CHECK IF IT DID ITS JOB 

     System.out.println("The students' names in alphabetical order: "); 

     for (int i = 0; i < names.length; i++) 
     { 
     System.out.println(names[i]); 
     } 


    //Ask user to enter the corresponding project scores 

     System.out.println("Enter the 3 project " 
          + "scores for each student: "); 

     for (int row = 0; row < scores.length; row++) 
     { 
     for (int i = 0; i < names.length; i++) 
      { 
      for (int col = 0; col < scores[row].length; col++) 
      { 
       System.out.print(names[i] + " Test " 
           + (col +1) + ": "); 
       scores[row][col] = scnr.nextInt(); 
      } 
      } 
      break; 
      } 


    //PRINT NAMES AND SCORES SIDE BY SIDE 
    //MAKE TABLE HEADING HERE 
     for (int row = 0; row < scores.length; row++) 
     { 
     for (int i = 0; i < names.length; i++) 
     { 
      System.out.println(names[i] + " grades: "); 
      for (int col = 0; col < scores[row].length; col++) 
      { 
      System.out.print(scores[row][col] + " "); 
      } 
      System.out.println(); 
     }break; 
     } 
    } 



/**Selection sort method made to sort the student names in 
* alphabetical order. 
@param names names of students 
@return the names organized alphabetically 
**/ 

public static String[] selectionSort(String[] names) 
{ 
    for (int index = 0; index < names.length - 1; ++index) 
    { 
    int minIndex = index; 
     for (int j = index + 1; j < names.length; ++j) 
     { 
     if (names[j].compareTo(names[minIndex]) < 0) 
     { 
      minIndex = j; 
     } 
     } 
    String temp = names[index]; 
    names[index] = names[minIndex]; 
    names[minIndex] = temp; 
    } 
    return (names); 
    } 
} 

我該如何編輯這樣才能讓每個學生都有正確的分數? 預先感謝您的幫助!

+0

表應該是什麼樣子? –

+0

完成此操作的一種方法不是返回一個字符串數組,而是讓您的'selectionSort()'直接對數組執行操作並讓int數組模仿對字符串數組執行的操作 –

回答

0

請注意,scores數組和名稱數組是相關的。因此,如您所做的那樣,輸入名稱,對它們進行排序,然後要求用戶輸入分數。這意味着在排序名稱時,你不必四處遊戲。這意味着

names[i] has the scores in scores[i][0...2] 

for(int i = 0; i < names.length; ++i) 
    print names[i] + " : " + scores[i][0] + ", " + scores[i][1] + ", " + scores[i][2] ; 

See here

此外,您可能想要使用固定寬度。查找輸出格式化程序以正確格式化表格。

理想情況下,你想創建一個包含該名稱的類和三個標記作爲數據並實現自定義Comparator,那麼就存儲在一個ArrayList名單和排序它(這w.r.t字典順序的名稱進行比較)。

編輯 從(和註釋中)你的問題,你想先排序取輸入,這意味着你需要周圍的分數耍弄以同樣的方式,你會爲這樣做名字即如果names[i]names[j]被交換,那麼應該scores[i]scores[j]

+0

很確定目標是將數據* *輸入所有數據。嘗試輸入姓名,排序姓名,然後要求分數不符合該目標。 –

+0

是的,我也這麼認爲。但是我的代碼在OP代碼上做了很少的手術。添加一個編輯。 –

+0

非常感謝您的回覆! (並抱歉回來這麼晚),但既然你回答了我如何格式化表的問題,我會接受你的答案。 我對此有更多疑問,但我會在另一個帖子上詢問。 再次感謝您! – egg