課程
/后端開發
/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){ ????????//定義數組用以存儲前三名成績 ????????int[]?top?=?new?int[3]; ???????? ????????System.out.println("原始成績是:\n"?+?Arrays.toString(scores)); ???????? ????????//排序 ????????Arrays.sort(scores); ???????? ????????System.out.println("排序后的成績是:\n"?+?Arrays.toString(scores)); ????????//原始成績數組編號,為保證下一個循環繼續往后跳,所以這里提前定義,且為了保證從最大值讀取,所以這里初始值定義為數組長度-1 ????????int?i=scores.length?-1; ???????? ????????//j定義為為前三名數組編號 ????????for(int?j=0;j<3;j++){ ????????????while(i>=0){ ????????????????//如果成績不在0-100內,跳至下一個(i--),不做賦值 ????????????????if(scores[i]<0?||?scores[i]>?100){ ????????????????????i--; ????????????????????continue; ????????????????} ????????????????//成績在0-100內,賦值給top數組,并跳出循環,并保證下一個循環內成績繼續往后查詢(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基礎萌新入門第一課,從Java環境搭建、工具使用、基礎語法開始
3 回答和參考答案對過,感覺沒有地方出錯。
6 回答參考答案出錯了。。。
1 回答能給個正確答案參考一下嗎
1 回答感覺和參考答案一模一樣了還是有bug。
1 回答參考答案——簡單問題簡單處理
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
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
例如?