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

熱點內容
表格怎麼添加一行數據 瀏覽:35
旅遊網站開發怎麼選 瀏覽:669
properties文件位置 瀏覽:679
不想學編程可以做什麼 瀏覽:22
手機照片一鍵壓縮成文件 瀏覽:962
女生微信霸氣個性簽名 瀏覽:626
微博如何看別人一個月的數據 瀏覽:14
萬能小組件怎麼點擊就進入app應用 瀏覽:817
蘋果5s變形怎麼恢復 瀏覽:286
電腦里文件密碼忘了咋辦啊 瀏覽:315
輸入文件夾 瀏覽:31
extjsgrid刪除列 瀏覽:43
gdx文件怎麼打開 瀏覽:759
如何更新大數據量表 瀏覽:311
特殊量產工具 瀏覽:980
安卓網路顯示h2是什麼意思 瀏覽:90
哪個拍照app好 瀏覽:70
文件被設置隱藏屬性如何顯示 瀏覽:142
網路電視的好處 瀏覽:657
cf槍戰王者手游下載官網下載最新版本下載 瀏覽:925

友情鏈接