導航:首頁 > 編程語言 > 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相關的資料

熱點內容
喪屍電視劇哪個app看 瀏覽:196
電腦版上傳視頻找不到文件在哪 瀏覽:164
javastringtemplate 瀏覽:594
聯想啟天m7300能升級內存嗎 瀏覽:698
精雕怎麼打開圖片文件 瀏覽:161
ug編程鏜孔g76如何使用 瀏覽:719
7歲到18歲少兒編程學什麼 瀏覽:413
mac文件怎麼列印 瀏覽:756
農葯登記許可證查詢哪個網站 瀏覽:857
素材站用什麼網站系統 瀏覽:173
ug如何用鉸刀編程 瀏覽:647
三國志版本介紹 瀏覽:260
情侶用的定位app有哪些 瀏覽:244
怎麼清楚蘋果手機的健身數據 瀏覽:988
淘寶美工教程自學網 瀏覽:452
父子2006未刪減版本 瀏覽:872
包頭誰賣蘋果6 瀏覽:504
化工企業停工有哪些文件通知要求 瀏覽:878
下載軟體後文件夾在桌面 瀏覽:397
word文字編組 瀏覽:167

友情鏈接