我如何给同一个学生提供相同的职位

时间:2017-04-03 20:54:56

标签: java

我已经尝试了几次,但仍然没有得到,这是我的第一个java代码。用户输入学生的详细信息,如名称,ID和主题标记,然后在GUI中显示,学生结果按降序排列,如第1,第2等位置,但问题是现在具有相同标记的学生应该有相同的位置,但我的相反。任何帮助将不胜感激

public class Student  {

    private String id,sn,ln;

    private double eng,math,social,science;


    public Student(){

    }     
    public void setId(String id){
        this.id=id;
    }
    public void setSn(String s){
        this.sn=s;
    }
    public void setLn(String s){
        this.ln=s;
    }
    public void setEng(double en){
        this.eng=en;
    }       
    public void setMath(double ma){
        this.math=ma;  
    } 
    public void setSocial(double so){
        this.social=so;
    } 
    public void setScience(double sc){
        this.science=sc;
    }   
  public String getSid(){
     return id;
  }  
  public String getSname(){
     return sn;
  }          
  public String getLname(){
     return ln;
  }        
  public double getEng(){
            return eng;
  }
  public double getMath(){
      return math;
  }
  public double getSocial(){
      return social;
  }
  public double getScience(){
      return science;
  }      

  public double getAverage(){
      return (getEng()+getMath()+getSocial()+getScience())/4.0;
  }

  public double getTotalMark(){
     return    getEng()+getMath()+getSocial()+getScience();
 }
    int position;

 public String getPosition(int x){
     position=x;
      switch (position){   
     case 1:
          case 21:
               case 31:
                    case 41:            
      return position+"st";
          case 2:
               case 22:
                    case 32:
                         case 42:
      return position+"nd";
               case 3:
                   case 23:
                       case 33:
                           case 43:
      return position+"rd";
     default:  
       return position +"th";
 }
 }

     public String header(){
             // returns the header of the ressult
        return "STUDENT ID\t STUDENT NAME  \t ENGLISH \t MATH \t SOCIAL \t SCIENCE "
                + "\t TOTALMARK \t AVERAGE  \t POSITIONS\n";

          }
            @Override
          public String toString(){
                // display all the values of the student 
          return getSid()+"\t\t"+getSname()+" "+getLname()+
                  "  \t\t"+getEng()+"\t"+getMath()+"\t "+getSocial()+
                  "\t"+getScience()+"\t"+getTotalMark()+
                  "\t\t"+getAverage();
          }

这是我的排序方法。因为平均值是双倍,55.0的学生被评为高于55.5的学生。如果我尝试将其更改为双倍

,我会收到错误
import java.util.Comparator;

public class sortAverage implements Comparator <Student> {

      @Override
              public int compare(Student s1,Student s2){

            return (int) (s2.getAverage()-s1.getAverage());

      }
}

这是我调用我的代码的地方

public class thehandler implements ActionListener{
       @Override
          public void actionPerformed(ActionEvent ev){

                 Student student=new Student();

          if(ev.getSource()==addData){

              if(listof.size() <= 4){

              try{   

                  student.setEng(Double.parseDouble(eng.getText().toString()));
                  student.setScience(Double.parseDouble(sci.getText().toString()));
                  student.setSocial(Double.parseDouble(soc.getText().toString()));
                  student.setMath(Double.parseDouble(math.getText().toString()));}
              catch(Exception e){

           JOptionPane.showMessageDialog(null, "one or more of the subject fields contains invalid inputs,PLEASE NUMBER REQUIRED, please check it", "error", JOptionPane.ERROR_MESSAGE);

           return;
      }
                  student.setId(sidInput.getText().toString());
                  student.setSn(snameInput.getText().toString());
                  student.setLn(lnameInput.getText().toString());

                  listof.add(student);

                  sidInput.setText("");
                  snameInput.setText("");
                  lnameInput.setText("");
                  eng.setText("");
                  math.setText("");
                  soc.setText("");
                  sci.setText("");
                 }else{

                   JOptionPane.showMessageDialog(null, "The maximum Number of Student that can be added is 50 ",
                           "ERROR",JOptionPane.ERROR_MESSAGE);


                   return;
              }   

                Collections.sort(listof, new sortAverage());

             }
             else if(ev.getSource()==viewData){

                 display.setText(student.header());

                display.setFont(new Font("serif",Font.BOLD,16));


               int x=0;

               for(Student of:listof){
                  x++; 

              display.append(of.toString()+"\r\t"+of.getPosition(x)+"\r\n");

            }


             }

          }

      }

2 个答案:

答案 0 :(得分:0)

由于doublesortAverage值的比较不正确,您的排序结果无效。将double转换为int是不可接受的,因为它会忽略小数部分。例如。

(int) (55.5-55.0) 

将为0而不是0.5,并且两个值将相等。 使用Double.compare()方法:

import java.util.Comparator;

public class sortAverage implements Comparator <Student> {

  @Override
          public int compare(Student s1,Student s2)
  {
          return Double.compare(s2.getAverage(),s1.getAverage());
  }
}

答案 1 :(得分:0)

您应该将上一个for循环更改为如下所示:

int x=0;
Student lastStudent = null;
for (Student of:listof) {
    if (lastStudent == null || Double.compare(of.getAverage(), lastStudent.getAverage()) != 0) {
        x++;
    }
    display.append(of.toString()+"\r\t"+of.getPosition(x)+"\r\n");
    lastStudent = of;
}

使用Double.compare(s1.getAverage(), s2.getAverage())而不是简单地减去Comparator中的值,因为舍入错误更安全。