显示得分最高的学生

时间:2018-11-04 13:02:20

标签: java

  

编写一个程序,提示用户输入学生人数以及每个学生的姓名和分数,最后显示得分最高的学生。

我坚持如何显示他们的名字?

这是我的代码:

package Exercises;
import java.util.Scanner;
public class Page93 
{

    public static void main(String[] args) 
    {
        String name = null;
        int count;
        double score = 0;
        double highest = 0;

        Scanner input = new Scanner (System.in);

        System.out.print("Enter the number of student : ");
        int numberofstudent = input.nextInt();

        for (count=0; count<numberofstudent; count++)
        {
            System.out.print("\nStudent name : ");
            name = input.next().toUpperCase();
            System.out.print("Score        : ");
            score = input.nextInt();

            if (highest<score)
                highest=score;
        }
        System.out.print("\nThe highest score : " + highest );
    }
}

3 个答案:

答案 0 :(得分:1)

定义变量 studentWithHighestScore 以存储得分最高的学生。每当您更新最高时,都要更新此变量。

   if (highest<score) {
      highest=score;
      studentWithHighestScore = name
   }

答案 1 :(得分:-1)

package Exercises;
import java.util.Scanner;
public class Page93 
{


   public static void main(String[] args) 
    {
        String name = null;
        int count;
        double score = 0;
        double highest = 0;
        String highestName;

        Scanner input = new Scanner (System.in);

        System.out.print("Enter the number of student : ");
        int numberofstudent = input.nextInt();

        for (count=0; count<numberofstudent; count++)
        {
            System.out.print("\nStudent name : ");
            name = input.next().toUpperCase();
            System.out.print("Score        : ");
            score = input.nextInt();

            if (highest<score)
            {
              highest=score;
              highestName = name;
            }
        }
        System.out.print("\nThe highest student : " + highestName + " score : " + highest );
    }
}

答案 2 :(得分:-1)

import java.util.Scanner;
class Student {
    String name;
    String stu_id;
    int score;
    public Student() {
    }
    public Student(String initName, String initId, int initScore) {
        name = initName;
        stu_id = initId;
        score = initScore;
    }
}
class accept {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Input number of students:");
        int n = Integer.parseInt(in.nextLine().trim()) ;
        System.out.println("Input Student Name, ID, Score :");
        Student stu = new Student();
        Student max = new Student();
        Student min = new Student("","",  0);
        String [] arr1=new String [n];
        String [] arr2=new String [n];
        int [] arr3=new int [n];
        for (int i = 0; i < n; i ++) {
        arr1[i]=in.next();
        arr2[i]=in.next();
        arr3[i]=in.nextInt();
            stu.name = arr1[i];
            stu.stu_id = arr2[i];
            stu.score = arr3[i];
        if (max.score < stu.score) {
            max.name = stu.name;
            max.stu_id = stu.stu_id;
            max.score = stu.score;              }}
    for(int j = 0; j < n; j ++){
            stu.name = arr1[j];
            stu.stu_id = arr2[j];
            stu.score = arr3[j];
        if (min.score < stu.score&&stu.score!=max.score) {
            min.name = stu.name;
            min.stu_id = stu.stu_id;
            min.score = stu.score;
            
            }
}
        System.out.println("name, ID of the highest score and the second highest score:");
System.out.println(max.name + " " + max.stu_id);
System.out.println(min.name + " " + min.stu_id);
        in.close();
    }
}
相关问题