課程
/后端開發(fā)
/Java
/Java入門第一季(IDEA工具)升級版
我怎么感覺這個題目給出的答案有點問題???你們有問題嗎?伙計們
2016-10-15
源自:Java入門第一季(IDEA工具)升級版 7-1
正在回答
import?java.util.Arrays; public?class?HelloWorld?{ ????public?static?void?main(String[]?args)?{ ????????HelloWorld?hello?=?new?HelloWorld(); ????????int[]?scores?=?{9,-23,64,91,119,52,73}; ????????//接收返回的前三名成績 ????????int[]?top3?=?hello.getSorted(scores); ????????//輸出前三名 ????????System.out.println("前三名的成績是:\n"?+?Arrays.toString(top3)); ????} ????public?int[]?getSorted(int[]?scores){ ????????//定義數(shù)組用以存儲前三名成績 ????????int[]?top?=?new?int[3]; ???????? ????????System.out.println("原始成績是:\n"?+?Arrays.toString(scores)); ???????? ????????//排序 ????????Arrays.sort(scores); ???????? ????????System.out.println("排序后的成績是:\n"?+?Arrays.toString(scores)); ????????//原始成績數(shù)組編號,為保證下一個循環(huán)繼續(xù)往后跳,所以這里提前定義,且為了保證從最大值讀取,所以這里初始值定義為數(shù)組長度-1 ????????int?i=scores.length?-1; ???????? ????????//j定義為為前三名數(shù)組編號 ????????for(int?j=0;j<3;j++){ ????????????while(i>=0){ ????????????????//如果成績不在0-100內(nèi),跳至下一個(i--),不做賦值 ????????????????if(scores[i]<0?||?scores[i]>?100){ ????????????????????i--; ????????????????????continue; ????????????????} ????????????????//成績在0-100內(nèi),賦值給top數(shù)組,并跳出循環(huán),并保證下一個循環(huán)內(nèi)成績繼續(xù)往后查詢(i--) ????????????????top[j]?=?scores[i]; ????????????????i--; ????????????????break; ????????????} ????????} ????????return?top; ????} }
我自己想了想,分享下:
import java.util.Arrays;
public class Homework1 {
????public static void main(String[] args) {
????????Homework1 hw=new Homework1();
????????int []scores=new int[]{ 89 , -23 , 64 , 91 , 119 , 52 , 73};
????????System.out.println("考試成績前三名為:");
????????????hw.showTop3(scores);
????}
????????????public void showTop3(int[] scores){
????????????????????int count=0;
????????????????Arrays.sort(scores);
????????????????for(int i=scores.length-1;i>=0;i--){
????????????????if(scores[i]>100||scores[i]<0){
????????????????????????continue;
?????????????????}else{
????????????????????????count++;
????????????????????????}
????????????????if(count>3){
????????????????????break;
?????????????????}
????????????System.out.println(scores[i]);
????????}
}
例如?
舉報
0基礎(chǔ)萌新入門第一課,從Java環(huán)境搭建、工具使用、基礎(chǔ)語法開始
6 回答參考答案出錯了。。。
3 回答和參考答案對過,感覺沒有地方出錯。
1 回答能給個正確答案參考一下嗎
1 回答感覺和參考答案一模一樣了還是有bug。
1 回答參考答案——簡單問題簡單處理
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2016-10-17
2016-10-15
我自己想了想,分享下:
import java.util.Arrays;
public class Homework1 {
????public static void main(String[] args) {
????????Homework1 hw=new Homework1();
????????int []scores=new int[]{ 89 , -23 , 64 , 91 , 119 , 52 , 73};
????????System.out.println("考試成績前三名為:");
????????????hw.showTop3(scores);
????}
????????????public void showTop3(int[] scores){
????????????????????int count=0;
????????????????Arrays.sort(scores);
????????????????for(int i=scores.length-1;i>=0;i--){
????????????????if(scores[i]>100||scores[i]<0){
????????????????????????continue;
?????????????????}else{
????????????????????????count++;
????????????????????????}
????????????????if(count>3){
????????????????????break;
?????????????????}
????????????System.out.println(scores[i]);
????????}
????}
}
2016-10-15
例如?