當前位置:首頁 » 編程語言 » java死機

java死機

發布時間: 2025-03-02 17:24:58

⑴ 我寫了一個java程序,一運行就死機

不僅Chinese_Score類中的死循環,還有(int) (Math.random() * 60) + 40
如果(int) Math.random() * 60+ 40,Math.random()的值[0,1)取整恆為0,你的平均值就永遠為40,所以應:(int) (Math.random() * 60),具體如下:

public class Chinese_Score {

private int[] Chinese;

public Chinese_Score() {
Chinese = new int[20];
int i = 0;
while (i < 20) {
Chinese[i] = (int) (Math.random() * 60) + 40;
i++;
}
}

public int Average_Chinese() {
int total = 0;
for (int i = 0; i < 20; i++) {
total += Chinese[i];
}
return (int) total / 20;
}
}

public class English_Score {

private int[] English;

public English_Score() {
English = new int[20];
int i = 0;
do {
English[i] = (int) (Math.random() * 60) + 40;
} while (++i < 20);
}

public int Average_English() {
int total = 0;
for (int i = 0; i < 20; i++) {
total += English[i];
}
return (int) total / 20;
}
}

public class Math_Score {

private int[] Maths;

public Math_Score() {
Maths = new int[20];
for (int i = 0; i < 20; i++) {
Maths[i] = (int) (Math.random() * 60) + 40;
}
}

public int Average_Maths() {
int total = 0;
for (int i = 0; i < 20; i++) {
total += Maths[i];
}
return (int) total / 20;
}
}

public class Main {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Math_Score math = new Math_Score();
Chinese_Score chinese = new Chinese_Score();
English_Score english = new English_Score();

System.out.println("Math :" + math.Average_Maths());
System.out.println("Chinese :" + chinese.Average_Chinese());
System.out.println("English :" + english.Average_English());
}
}

熱點內容
scratch少兒編程課程 發布:2025-04-16 17:11:44 瀏覽:642
榮耀x10從哪裡設置密碼 發布:2025-04-16 17:11:43 瀏覽:369
java從入門到精通視頻 發布:2025-04-16 17:11:43 瀏覽:89
php微信介面教程 發布:2025-04-16 17:07:30 瀏覽:312
android實現陰影 發布:2025-04-16 16:50:08 瀏覽:795
粉筆直播課緩存 發布:2025-04-16 16:31:21 瀏覽:348
機頂盒都有什麼配置 發布:2025-04-16 16:24:37 瀏覽:213
編寫手游反編譯都需要學習什麼 發布:2025-04-16 16:19:36 瀏覽:819
proteus編譯文件位置 發布:2025-04-16 16:18:44 瀏覽:369
土壓縮的本質 發布:2025-04-16 16:13:21 瀏覽:596