課程
/后端開發(fā)
/Java
/Java入門第一季(IDEA工具)升級版
這個代碼對照了下標準答案,除了方法名不一樣,其他都按標準答案填了,但還是報錯,求看看錯在哪里
2015-07-03
源自:Java入門第一季(IDEA工具)升級版 7-1
正在回答
hello.top(socres)拼寫錯誤 ?應(yīng)該為:hello.top(scores) ?要和你main里面定義的數(shù)組名一致。第9行
int num=0;寫錯了。用myeclipse編譯時顯示錯誤。因為如果符合if的話,接下來就執(zhí)行了continue,跳出if后的語句進入下一個循環(huán)。大括號的位置理一理就好,樓上給出了正確代碼。
首先,這樣寫fou循環(huán)中不會執(zhí)行任何東西。因為if中continue,后面的代碼會自動跳過,執(zhí)行下一次for循環(huán)。
然后。int num=0;寫在 for循環(huán)中每次都會循環(huán)執(zhí)行。所以num永遠等于0.sort排序由小到大進行排序,所以輸出的結(jié)果為成績最小的三位。正確的為:
? ?public static void top(int[] scores){
? ? ? ? Arrays.sort(scores);//排序
? ? ? ? int num =0;
? ? ? ? for(int i=scores.length-1;i>0;i--){
? ? ? ? ? ?if(scores[i]<0||scores[i]>100){
? ? ? ? ? ? ? ? continue; ? ? ? ? ??
? ? ? ? ? }
? ? ? ? ? ?num++;
? ? ? ? ? ?if (num>3){?
? ? ? ? ? ? ? ?break; ??
? ? ? ? ? ?}
? ? ? ? ? System.out.println(scores[i]);
? ? ? ?}?
? ? }
package cn.edu.zafu;
import java.util.Arrays;
public class llll {
?/**? * @param args? */?public static void main(String[] args) {??// TODO Auto-generated method stub?????int[] scores={1,2,5,6,3};?????? Arrays.sort(scores);//排序?????? for(int i=0;i<scores.length;i++)?????? {????? ????
????????????? if(scores[i]<0||scores[i]>100)????????????? {????????????? ?continue;????????????? }???????????????? int num=0;????????????? ?????????????? ?num++;????????????? ?if(num>3)????????????? ?{????????????? ?break;????????????? ?}????????????? ?System.out.println(scores[i]);
?????? }??
?}
這樣。。
public?void?top(int[]?scores){ ?????Arrays.sort(scores);//排序 ?????for(int?i=0;i<scores.length;i++){ ????????????if(scores[i]<0||scores[i]>100){ ?????????????????continue; ????????????int?num?=0; ????????????num++; ????????????if?(num>3){ ???????????????break; ?????????????} ??????????System.out.println(scores[i]); ??????} }
這個方法錯了吧 ? ?int ?num = 0 應(yīng)該寫在for循環(huán)外面 試試?
舉報
0基礎(chǔ)萌新入門第一課,從Java環(huán)境搭建、工具使用、基礎(chǔ)語法開始
1 回答幫我看看哪里錯了
4 回答幫我看看哪里錯了
1 回答哪里錯了幫我看下 謝謝
1 回答我這哪里錯了?幫我看看
3 回答幫我看看錯在哪里了
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2015-07-08
hello.top(socres)拼寫錯誤 ?應(yīng)該為:hello.top(scores) ?要和你main里面定義的數(shù)組名一致。第9行
int num=0;寫錯了。用myeclipse編譯時顯示錯誤。因為如果符合if的話,接下來就執(zhí)行了continue,跳出if后的語句進入下一個循環(huán)。大括號的位置理一理就好,樓上給出了正確代碼。
2015-07-04
首先,這樣寫fou循環(huán)中不會執(zhí)行任何東西。因為if中continue,后面的代碼會自動跳過,執(zhí)行下一次for循環(huán)。
然后。int num=0;寫在 for循環(huán)中每次都會循環(huán)執(zhí)行。所以num永遠等于0.sort排序由小到大進行排序,所以輸出的結(jié)果為成績最小的三位。正確的為:
? ?public static void top(int[] scores){
? ? ? ? Arrays.sort(scores);//排序
? ? ? ? int num =0;
? ? ? ? for(int i=scores.length-1;i>0;i--){
? ? ? ? ? ?if(scores[i]<0||scores[i]>100){
? ? ? ? ? ? ? ? continue; ? ? ? ? ??
? ? ? ? ? }
? ? ? ? ? ?num++;
? ? ? ? ? ?if (num>3){?
? ? ? ? ? ? ? ?break; ??
? ? ? ? ? ?}
? ? ? ? ? System.out.println(scores[i]);
? ? ? ?}?
? ? }
2015-07-03
package cn.edu.zafu;
import java.util.Arrays;
public class llll {
?/**
? * @param args
? */
?public static void main(String[] args) {
??// TODO Auto-generated method stub
??
???int[] scores={1,2,5,6,3};
?????? Arrays.sort(scores);//排序
?????? for(int i=0;i<scores.length;i++)
?????? {
????? ????
????????????? if(scores[i]<0||scores[i]>100)
????????????? {
????????????? ?continue;
????????????? }
???????????????? int num=0;
????????????? ?
????????????? ?num++;
????????????? ?if(num>3)
????????????? ?{
????????????? ?break;
????????????? ?}
????????????? ?System.out.println(scores[i]);
?????? }
??
?}
這樣。。
2015-07-03
這個方法錯了吧 ? ?int ?num = 0 應(yīng)該寫在for循環(huán)外面 試試?