Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 按字母顺序在并行数组中排序相似的分数_Java_Arrays_Sorting - Fatal编程技术网

Java 按字母顺序在并行数组中排序相似的分数

Java 按字母顺序在并行数组中排序相似的分数,java,arrays,sorting,Java,Arrays,Sorting,对于一项作业,我需要要求一些学生。提示用户输入学生的姓氏和分数,然后按降序排列分数并显示。我已经做了所有这些,但我不知道如何按字母顺序排序的名称,如果他们有相同的分数 输入示例: 布莱恩11 约翰33 菲尔22 乔22 亚当33 我的输出: 约翰33 亚当33 菲尔22 乔22 布莱恩11 我需要的输出: 亚当33 约翰33 乔22 菲尔22 布莱恩11 我无法获得所需的输出。我通常能解决这类问题,但这件事让我心烦意乱。。请帮忙 import java.util.*; public cl

对于一项作业,我需要要求一些学生。提示用户输入学生的姓氏和分数,然后按降序排列分数并显示。我已经做了所有这些,但我不知道如何按字母顺序排序的名称,如果他们有相同的分数

输入示例:

布莱恩11

约翰33

菲尔22

乔22

亚当33


我的输出:

约翰33

亚当33

菲尔22

乔22

布莱恩11


我需要的输出:

亚当33

约翰33

乔22

菲尔22

布莱恩11


我无法获得所需的输出。我通常能解决这类问题,但这件事让我心烦意乱。。请帮忙

import java.util.*;

public class className
{
    public static void main(String[] args)
    {
        //Scanner input for # of students
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the number of students: ");
        int numofstudents = input.nextInt();
        System.out.println();

        //Initializes an array of strings to the size of the # of students
        String[] names = new String[numofstudents];

        //Initializes an array of ints to the size of the # of students
        int[] array = new int[numofstudents];

        //For loop which asks for each students name and score until # of students reached
        for(int i = 0; i < numofstudents; i++)
        {
            System.out.print("Enter the student's lastname: ");
            names[i] = input.next();
            System.out.print("Enter the student's score: ");
            array[i] = input.nextInt();
            System.out.println();
        }
        //Sorts the arrays for names and scores of students
        selectionSort(names, array);
        //Output
        System.out.println();
        System.out.println();
        System.out.println("Number of Students: " + numofstudents);
        //System.out.println(Arrays.toString(names));
        for(int i = 0; i < numofstudents; i++)
        {
            System.out.println(names[i] + ": " + array[i]);
        }
    }

    public static void selectionSort(String[] names, int[] array) {
        for(int i = array.length - 1; i >= 1; i--) {
            String temp;
            int currentMax = array[0];
            int currentMaxIndex = 0;
            for(int j = 1; j <= i; j++) {
                if (currentMax > array[j]) {
                    currentMax = array[j];
                    currentMaxIndex = j;
                }
            }
                if (currentMaxIndex != i) {
                    temp = names[currentMaxIndex];
                    names[currentMaxIndex] = names[i];
                    names[i] = temp;
                    array[currentMaxIndex] = array[i];
                    array[i] = currentMax;
                }
        }
    }
}
import java.util.*;
公共类类名
{
公共静态void main(字符串[]args)
{
//为#个学生输入扫描仪
扫描仪输入=新扫描仪(System.in);
System.out.print(“输入学生人数:”);
int numofstudents=input.nextInt();
System.out.println();
//将字符串数组初始化为#个学生的大小
字符串[]名称=新字符串[numofstudents];
//将整数数组初始化为#个学生的大小
int[]数组=新int[numofstudents];
//For循环,要求每个学生的姓名和分数,直到#个学生达到
for(int i=0;i=1;i--){
字符串温度;
int currentMax=数组[0];
int currentMaxIndex=0;
对于(int j=1;j数组[j]){
currentMax=数组[j];
currentMaxIndex=j;
}
}
如果(currentMaxIndex!=i){
temp=名称[currentMaxIndex];
名称[currentMaxIndex]=名称[i];
名称[i]=临时工;
数组[currentMaxIndex]=数组[i];
数组[i]=currentMax;
}
}
}
}

实际上有两种排序方法要做。开始按字母顺序排序数组,无论分数是多少。稍后,根据分数对数组进行排序,得到结果


编辑:在根据分数排序时,当t[i]=t[i-1]

这听起来像是可行的。我会尝试一下,让您知道它是如何工作的。通常假设两个数组中的值是相关的。这意味着对它们单独排序不是一个好主意。是的,Peter,我这里的排序意味着对字母和数字排序中的两个数组进行相同的操作。