2016-03-02 122 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