2013-10-20 34 views
0

过去几天我一直在研究此程序,并确切知道我想要做什么,而不是如何去做。基本上我有两个数组,一个是包含学生姓名的字符串,另一个是包含学生成绩的int数组。这两个数组值都是用户输入的输入。最终,我想打印出相应的名称和分数,从最高分到最低分按降序排列。现在,我的问题在于代码的结尾,在这个代码中我无法理解如何让两个索引匹配println的用途(我已经评论了问题的出处在哪里,我打印的分数是下降顺序的从最高到最低的,但我不能得到遵守的名字。如何解决这个问题的任何建议,将不胜感激。如何获取字符串数组索引以匹配Int中的Int数组索引

import java.util.Scanner; 

public class TestArray 
{ 
    public static void main(String[] args) 
    { 

     Scanner input = new Scanner(System.in); 

     System.out.println("Enter the number of students in your class: "); 
     int number = input.nextInt(); 

     System.out.println("Now enter the " + number + " students names"); 
     String[] nameList = new String[number]; 

     for (int i = 0; i < number; i++) { 
      nameList[i] = input.next(); 
     } 

     System.out.println("Next, enter the score of each student: "); 
     int[] numGrades = new int[number]; 
     for (int i = 0; i < number; i++) { 
      numGrades[i] = input.nextInt(); 
     } 

     for (int i = 0; i < number; i++) { 
      int currentMax = numGrades[i]; 
      int currentMaxIndex = i; 
      int currentNameIndex = i; 

      for (int j = i + 1; j < number; j++) { 
       if (currentMax < numGrades[j]) { 
        currentMax = numGrades[j]; 
        currentMaxIndex = j; // index max 
        currentNameIndex = j; 
       } 
      } 

      if (currentMaxIndex != i) { 
       numGrades[currentMaxIndex] = numGrades[i]; 
       numGrades[i] = currentMax; 
      } 

      if (currentNameIndex != i) { 
       nameList[currentNameIndex] = String.valueOf(nameList[i]); 
       // need nameList[i] to = the current index of the numGrades array HOW??? 
      } 
     } 

     for (int i = 0; i < number; i++) { 
      System.out.println(nameList[i] + " had a score of " + numGrades[i]); 
     } 
    } 
} 
+2

如果您已经学过类,那么最好将分数和名称放入类中,并使用该类的单个对象数组。 – ApproachingDarknessFish

回答

3

为了这个工作,你必须保持阵列同步。当你移动一个等级,你必须以完全相同的方式移动相应的名字,这样名称和等级排列总是一致的。

你可能已经明白,很难从代码中知道如果是这样,你将需要eith呃在同一个块({})中移动名称,就像移动等级一样,这会最有意义,或者您需要在某处存储必要的整数,以便另一个块可以使用它们。


除了回答:

你有两个阵列,像值

peter 45 
alice 53 
garth 50 

(我们希望这不是100分的考试)

你是排序第二个数组;你的最大值等等,都与该阵列中的分数有关。

假设您达到了将garth的分数与alice分数交换的点。在某些变量或其他变量中,对于garth和1对于爱丽丝,你将得到一个值,并且你将把50放在一个临时变量中,将爱丽丝的53放在位置2上,并将加斯的50放在位置1上。

什么你所要做的就是利用这些相同的指数移动的名称,所以你把加思在位置1和Alice在位置2大功告成后,您有:

peter 45 
garth 50 
alice 53 

你不需要任何额外的字符串位置的变量;您需要将字符串放入字符串数组中与您在分数数组中移动的分数相同的位置。

+0

感谢您回复rcook。你说什么是有道理的,而且大部分是我正在做的。我只是很难弄清楚如何让字符串索引等于整数,因为我没有办法看到像字符串数组值这样的最大值。 – censortrip

1

看来你有一些sorting实施正在进行,所以这是我的建议。


1.当你把学生和成绩分到不同的数组中时,他们应该按索引匹配,我相信你是这样做的。


2.在排序实现中,当对排列数组进行排序时,对于您交换的每个成绩索引,对names数组索引执行相同操作。这将让你的两个数组相同的指标尽可能的匹配对

if(currentMaxIndex != i){   
     numGrades[currentMaxIndex] = numGrades[i]; 
     numGrades[i] = currentMax; 
     // also swap the student name indices      
     // you indices should be the same for both arrays          
} 
//you don't need the other if statement, because you are swapping in the one above 

在你的循环,currentNameIndex应该等于currentMaxIndex,所以你真的不需要currentNameIndex。当您换入if声明时,请使用currentMaxIndex进行阵列交换。

编辑:与交换

// Simple swap 
int n = 1; 
int n1 = 2; 

int temp = n; // temp holds value of n (1) 
n = n1; // now n = 2 
n1 = temp // now n1 = 1 

上面的互换例子,做同样的你的名字排列,使用currentMaxIndex

+0

感谢您的回复peeskillet,我做了您的第一个建议,并试图为第二个做同样的事情,但是,我不知道如何获取名称数组索引与int数组交换。这是我遇到的全部问题。 – censortrip

+0

查看我的编辑与示例交换 –

+1

当你排序的档次也只是排序名称。如果您将等级[4]与等级[8]交换,也会将名称[4]与名称[8]交换。但是,您正在对成绩进行排序,也可以并排排列名称。 – Radiodef

1

有两种方法去解决这个问题,一个是你可以使用Map类来保存对应于学生名称和分数的名称值对。您需要查看javadoc的Map接口及其实现类(例如HashMap)。你会这样声明:

Map<String, Integer> nameGradeMap = new HashMap<String, Integer>(); 

Afterwords,对键值和散列表值进行排序。这可以通过调用Collections.sort()方法完成。

第二种选择是将学生姓名(字符串)和等级(整数)封装为某个类的实例变量,例如,学生。然后使用自定义比较来实现按名称排序,然后按分数排序。您需要阅读Comparable界面的javadoc并查看如何使用它的一些示例。再次,您可以使用致电Collections.sort()对其进行排序。我想这应该是足够的东西,让你看起来让你开始。

+0

感谢您的建议,我一定会检查一下,看看我能否解决我的问题。 – censortrip

0

仅供将来参考,以下是我提出的解决方案。请注意,我只更改了代码的最后一部分,并仅包含了下面的那一部分。感谢所有帮助过我的人。

(int i = 0; i < number; i++){ 
     int currentMax = numGrades[i]; 
     int currentMaxIndex = i;    
     int currentNameIndex = i; 
     String currentName = nameList[i]; 

     for (int j = i + 1; j < number; j++){ 
     if(currentMax < numGrades[j]){ 
      currentMax = numGrades[j];  
      currentMaxIndex = j;     
      currentNameIndex = j; 
      currentName = nameList[j]; 
      }   
     } 

     if(currentMaxIndex != i){   
      numGrades[currentMaxIndex] = numGrades[i]; 
      numGrades[i] = currentMax;               
      nameList[currentNameIndex] = String.valueOf(nameList[i]); 
      nameList[i] = String.valueOf(currentName);   
     } 

     }