打印出最高分数和学生姓名

时间:2019-01-21 23:19:56

标签: java

因此,我必须将10个名称和标记输入两个数组,并打印出最佳学生的名称和标记。我对Java还是很陌生,我只能从数组中打印出最大分数,但是如何将其链接到学生姓名?

        foreach($categoryTitleSearch_decoded as $x=>$x_value){
        if ($_GET['id'] == $x_value['category_id']){
            $x_value['category_name'] = $h2Title;
    } }

echo $h2Title;

2 个答案:

答案 0 :(得分:1)

我创建了一个班级学生:

public class Student {

    private double mark;
    private String name;

    public Student()
    {
        mark = 0;
        name = "";
    }

    public Student(int mark, String name)
    {
        this.mark = mark;
        this.name = name;
    }

    public double getMark()
    {
        return mark;
    }
    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public void setMark(double mark)
    {
        this.mark = mark;
    }

}

然后我在主目录中编辑了代码

public static void main(String[] args) {
    // TODO code application logic here

    ArrayList<Student> studs = new ArrayList<Student>();
    for(int i=0; i< 3; i++)
    {
        studs.add(new Student());
    }

    Scanner tomato = new Scanner(System.in);                                                                                                                                                                   

    double max;                                                                                                                                                                                             
    int i;                                                                                                                                                                                                     

    for(i=0; i<3; i++) {  
         System.out.println("Enter name of student: ");                                                                                                                                                                   
        studs.get(i).setName(tomato.nextLine()); 
        System.out.println("Enter marks: ");                                                                                                                                                                   
        studs.get(i).setMark(tomato.nextDouble()); 
        tomato.nextLine();
    }                         
    int position = 0;
    max = studs.get(0).getMark();                                                                                                                                                                                            
    for(i = 0; i < 3; i++) {                                                                                                                                                                                  
        if(max < studs.get(i).getMark()) {                                                                                                                                                                                   
            max = studs.get(i).getMark(); 
            position = i;
        }                                                                                                                                                                                                      
    }                                                                                                                                                                                                          
    System.out.println("Highest marks:"+studs.get(position).getMark() + " student name " + studs.get(position).getName());
}

但是,这不是最简单的选择

编辑:Simplier

   public static void main(String[] args) {
    // TODO code application logic here


    Scanner tomato = new Scanner(System.in);                                                                                                                                                                   
    double[] marks = new double[10];
    String[] names = new String[10];
    double max;                                                                                                                                                                                             
    int i;                                                                                                                                                                                                     

    for(i=0; i<3; i++) {  
         System.out.println("Enter name of student: ");                                                                                                                                                                   
        names[i] = (tomato.nextLine()); 
        System.out.println("Enter marks: ");                                                                                                                                                                   
        marks[i] = tomato.nextDouble(); 
        tomato.nextLine();
    }                         
    int position = 0;
    max = marks[i];                                                                                                                                                                                            
    for(i = 0; i < 3; i++) {                                                                                                                                                                                  
        if(max < marks[i]) {                                                                                                                                                                                   
            max = marks[i]; 
            position = i;
        }                                                                                                                                                                                                      
    }                                                                                                                                                                                                          
    System.out.println("Highest marks:"+ marks[position] + " student name " + names[position]);
}

答案 1 :(得分:0)

我已经对此进行了编辑,当我运行它时,我可以键入名字和标记,但是之后它会出现错误:

import java.util.Scanner;
public class storeMarks
{
public static void main (String[]args)
{
    Scanner tomato = new Scanner(System.in);
    double max;
    double marks[];
    String name[];
    int index = 0;

    marks= new double[10];
    name=new String[10];

    int i;
        for(i=0;i<10;i++){
                System.out.println("Enter name of student: ");
                name[i]=tomato.nextLine();
                System.out.println("Enter marks: ");
                marks[i]=tomato.nextDouble();
        }

    max = marks[0];
    for(i = 0; i < 10; i++)
        {
        if(max < marks[i])
        {
        max = marks[i];
        index = i;

        }
        }


        System.out.println("Student with highest marks is "+name[index]+" and the mark is "+max);
}