0

/* * (对学生进行排序) 编写一个程序,提示用户输入学生人数、*学生姓名和他们的分数,并按照分数的降序打印学生姓名。*/

package homework6_17;

import java.util.Scanner;

public class Homework6_17 {
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter number of students: ");
    int numberOfStudents = input.nextInt();
    String[] names = new String[numberOfStudents];
    for (int i = 0; i < numberOfStudents; i++) {
        System.out.println("Enter the name of student: ");            
        names[i] = input.nextLine();

    }
    double[] scores = new double[numberOfStudents];
    for (int i = 0; i < numberOfStudents; i++) {
        System.out.println("Enter the score of student: ");            
        scores[i] = input.nextDouble();
    }

    String temps = "";
    double temp = 0;
    double max = scores[0];
    for(int i = 0; i<(scores.length-1); i++){
        if(scores[i+1]>scores[i]){
             temp=scores[i+1];
            scores[i]=scores[i+1];
            scores[i+1]=scores[i];

            temps = names[i+1];
            names[i]=names[i+1];
            names[i+1]=names[i];
        }
    }
    for(int i = 0 ; i<(scores.length-1); i++)
        System.out.println(names[i]+ " " + scores[i]);        


}
}

当我运行这个程序时;跑:

输入学生人数:3

输入学生姓名: 输入学生姓名: a

输入学生姓名:b

输入学生的分数:c

Exception in thread "main" java.util.InputMismatchException

// 我得到了“输入学生姓名:”两次而不是一次。

4

3 回答 3

0

您只需要删除第一个System.out.print("Enter number of students: ");,因为您正在for为每个学生打印循环中的短语。因此,您为第一个学生打印了两次(一次在循环之前,一次在循环中)

于 2013-10-30T13:12:09.413 回答
0

首先想到的(不确定这里是否正确)是您输入学生人数并按“输入”。它读取第一个 int(3)并读取“enter”作为第一个学生的第一个输入。

也许试试int numberOfStudents = Integer.ParseInt(input.nextLine());?这样换行符就不会被添加到学生中。

于 2013-10-30T13:15:58.247 回答
0

在 SO 中回答作业问题不是一个好主意。但是既然你已经尝试了一些代码,那么回答Q就OK了。看一下:

import java.util.Scanner;

public class Homework6_17 {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.print("Enter number of students: ");
        int numberOfStudents = input.nextInt();
        String[] names = new String[numberOfStudents];

        for (int i = 0; i < numberOfStudents; i++) {
            System.out.println("Enter the name of student #" + (i + 1) + ":");
            names[i] = input.next();
        }

        double[] scores = new double[numberOfStudents];
        for (int i = 0; i < numberOfStudents; i++) {
            System.out.println("Enter the score of student " + names[i] + ":");
            scores[i] = input.nextDouble();
        }

        String tempName;
        double tempScore;
        for (int i = 0; i < numberOfStudents; i++) {
            for (int k = i + 1; k < numberOfStudents; k++) {
                if (scores[k] > scores[i]) {
                    tempName = names[i];
                    tempScore = scores[i];
                    names[i] = names[k];
                    scores[i] = scores[k];
                    names[k] = tempName;
                    scores[k] = tempScore;
                }
            }
        }

        for (int i = 0; i < numberOfStudents; i++)
            System.out.println(names[i] + " " + scores[i]);

    }
}
于 2013-10-30T13:43:37.587 回答