Anonymous
Anonymous asked in 電腦與網際網路程式設計 · 1 decade ago

JAVA程式求助...

public class student{

public string number;

public string name;

public int age;

public int[] score;

public void setNumber(string number){

this.number=number;

}

public void setName(string name){

this.number=name;

}

public void setAge(int Age){

this.age=age;

}

public void setScore(int[] score){

this.score=score;

}

public String getNumber(){

return number;

}

public String getNumber(){

return name;

}

public int getAge(){

return age;

}

public int[] getScore(){

return score;

}

public int getScoreTotal(){

int total=0;

for(int a:score){

total+=a;

}

return total;

}

public float getScoreTotal()/5.of;

}

public Student(){

this("A0000","Guest",18,new int{5});

}

public Student(String number,String name,int age,int[] score){

setNumber(number);

setName(name);

setAge(age);

setScore(score);

}

public static void main(String[] args){

Student s=new Student();

System.out.println(s.getNumber()+"\t"+s.getName()+"\t"+s.getAge()+"\t"+s.getScoreAve());

}

}

--------------------------------------------------------------------------------------------

1.改以Constructor來設定資料成員資料

2.利用陣列設定五位學生資料

3.定義一個以平均分數做為排序依據的方法

4.定義一個輸出學生資料(學號.姓名.五科成績.平均分數)的方法

5.將排序後的學生資料輸出(呼叫4.5次)

6.執行時輸入(學號)欲尋找的學生後,將找到之學生資料輸出(含平均分數)

要改成以上六點.....有誰會??????

Update:

把倒斜線 t 都轉為 \t,請自己再轉回來。<----這裡不太暸耶

1 Answer

Rating
  • 生鏽
    Lv 5
    1 decade ago
    Favorite Answer

    先宣告兩種排序方法:

    import java.util.Comparator;

    public class StudentOrderByScore implements Comparator<Student> {

    public int compare(Student arg0, Student arg1) {

    if(arg0.getAverageScore() < arg1.getAverageScore())

    return -1;

    else if(arg0.getAverageScore() > arg1.getAverageScore())

    return 1;

    return 0;

    }

    }

    以及

    import java.util.Comparator;

    public class StudentOrderByNumber implements Comparator<Student> {

    public int compare(Student arg0, Student arg1) {

    return(arg0.getNumber().compareTo(arg1.getNumber()));

    }

    }

    Student 類別:

    import java.io.*;

    import java.util.*;

    public class Student implements Comparator<Student> {

    public String number;

    public String name;

    public int age;

    public int[] score;

    public String getNumber(){

    return number;

    }

    public void setNumber(String number){

    this.number=number;

    }

    public String getName(){

    return name;

    }

    public void setName(String name){

    this.name=name;

    }

    public int getAge(){

    return age;

    }

    public void setAge(int age){

    this.age = age;

    }

    public int[] getScore(){

    return score;

    }

    public void setScore(int[] score){

    this.score=score;

    }

    public int getTotalScore(){

    int total=0;

    for(int a:score)

    total+=a;

    return total;

    }

    public float getAverageScore(){

    int total = getTotalScore();

    return (float)total / (float)score.length;

    }

    public Student() {

    }

    public Student(String number,String name,int age,int[] score){

    setNumber(number);

    setName(name);

    setAge(age);

    setScore(score);

    }

    public int compare(Student arg0, Student arg1)

    {

    if(arg0.getAverageScore() < arg1.getAverageScore())

    return -1;

    else if(arg0.getAverageScore() > arg1.getAverageScore())

    return 1;

    return 0;

    }

    2008-03-31 13:57:22 補充:

    public static void main(String[] args){

    Student[] students = new Student[5]; // declare an array to store 5 students' data

    // create the 5 students false data

    students[0] = new Student("10003", "student 3", 19, new int[] {30, 40, 50, 60, 70});

    2008-03-31 13:58:05 補充:

    students[1] = new Student("10005", "student 5", 21, new int[] {50, 60, 70, 80, 90});

    students[2] = new Student("10001", "student 1", 17, new int[] {10, 20, 30, 40, 50});

    students[3] = new Student("10004", "student 4", 20, new int[] {40, 50, 60, 70, 80});

    2008-03-31 13:58:25 補充:

    students[4] = new Student("10002", "student 2", 18, new int[] {20, 30, 40, 50, 60});

    // sort by average score and output

    Comparator comparator = new StudentOrderByScore();

    Arrays.sort(students, comparator);

    2008-03-31 13:58:50 補充:

    System.out.println("Number&#92;tName&#92;tAge&#92;tTotal&#92;tAverage");

    for(Student s : students)

    System.out.println(s.getNumber()+"&#92;t"+s.getName()+"&#92;t"+s.getAge()+"&#92;t"+s.getTotalScore()+"&#92;t"+s.getAverageScore());

    // sort by number

    comparator = new StudentOrderByNumber();

    2008-03-31 13:59:25 補充:

    Arrays.sort(students, comparator);

    // read the number

    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

    try {

    System.out.print("Enter the student number: ");

    String number = reader.readLine();

    2008-03-31 14:00:21 補充:

    Student key = new Student();

    key.setNumber(number);

    int index = Arrays.binarySearch(students, key, comparator);

    if(index >= 0) {

    Student s = students[index];

    System.out.println(s.getNumber()+"&#92;t"+s.getName()+"&#92;t"+s.getAge()+"&#92;t"+s.getTotalScore()+"&#92;t"+s.getAverageScore());

    }

    2008-03-31 14:00:48 補充:

    else {

    System.out.println("Not found!!!");

    }

    } catch (IOException e) {

    e.printStackTrace();

    }

    }

    }

    2008-03-31 14:01:55 補充:

    知識+ 把倒斜線 t 都轉為 &#92;t,請自己再轉回來。

    2008-04-01 16:42:46 補充:

    倒斜線 t 是定位符號,知識+ 在補充內容時無法輸入倒斜線,所以請你把上面的程式碼自行換回來倒斜線。

Still have questions? Get your answers by asking now.