私は数回試しましたが、まだ得られていません。これは私の最初の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();
}
これは私のソート方法です。平均が2倍であるため、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());
}
}
倍増し、それを変更しようとする場合、私は、これは私はあなたが原因sortAverage
クラスのdouble
値の不正確な比較の無効なソート結果を得ている私のコード
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");
}
}
}
}
ありがとうございました。今、完璧に働いています。今度は次のプロジェクトに進むことができます。 – Whiteag