導航:首頁 > 編程語言 > javaforstudents

javaforstudents

發布時間:2023-07-05 05:51:08

① 用java代碼設計一個學生類

public class Student{
private String name;
private String studentNo;
private float score;
//get方法
//set方法

public static void main(String[] args){
//你可以用學生數組或者List<Student>來存數據
//我示範一下用數組存數據的 當然你用list來存的話 後面的比較 更加簡單 只需要

//實現一個比較器即可
Student[] students = new Student[2]

Student a =new Student();
a.setName("a");
a.setStudentNo("001");
a.setScore(98);
students[0]=a;
Student b = new Student();
b.setName("b");
b.setStudentNo("002");
b.setScore(87);
sudents[1] = b;
。。。。。。

//然後用冒泡排序根據學生的成績判斷 演算法你自己決定 冒泡還是挺好用的
for(int i =0,len =students.length ;i<len;i++){
for(int j=i+1,len;=students.length;j<len;j++){

if(students[i].getScore()>students[j].getScore()){
Student temp =students[i];
students[i] = students[j];

students[j]=temp;
}

}
}
///最後輸出結果
for(){
System.out.println();
}
}

② Java中計算學生的平均身高

public class Compute {

class Student {// 學生類
private double height;

Student(double height) {
this.height = height;
}

public double getHeight() {
return height;
}

public void setHeight(double height) {
this.height = height;
}

}

class Height {// 方法類

public double compute(Student[] students) {
int count = students.length;
double sum = 0;
for (Student student : students) {
sum += student.getHeight();
}
double result = sum / count;
return result;
}
}

class TestHeihgt {// 測試類

public void test() {
Student[] students = { new Student(170), new Student(160), new Student(165) };
Height height = new Height();
double avgHeight = height.compute(students);
System.out.println("學生平均身高是:" + avgHeight);
}
}

public static void main(String[] args) {
TestHeihgt testHeihgt = new Compute().new TestHeihgt();
testHeihgt.test();
}
}

純手打,測試可用!

閱讀全文

與javaforstudents相關的資料

熱點內容
任命文件人字多少號 瀏覽:27
如何給桌面的文件自動填寫序號 瀏覽:349
壓縮文件JPG重命名 瀏覽:487
vivox7怎麼隱藏文件 瀏覽:340
官網買蘋果好處 瀏覽:153
直播app源碼如何搭建 瀏覽:31
c盤系統文件包介紹圖片 瀏覽:856
linuxopt主要存哪些文件 瀏覽:924
jsdate天數 瀏覽:930
qq空間怎樣發視頻文件在哪裡找 瀏覽:208
nrf2401收發程序 瀏覽:498
哪個網站看新聞最好 瀏覽:944
找到文件夾並清除linux 瀏覽:82
蘋果app不安全怎麼辦 瀏覽:1000
電腦桌面文件收集軟體推薦 瀏覽:317
桌面的文件夾不能刪除不了怎麼辦 瀏覽:724
什麼是手機溫控文件 瀏覽:840
廣東農信app怎麼更改預留手機號碼 瀏覽:241
cdm打開隱藏文件夾 瀏覽:320
貝殼app房子為什麼偶爾不顯示 瀏覽:4

友情鏈接