幫忙看看為什么錯吧,腦子無法思考了
import java.util.Arrays;
public class HelloWorld {
? ??
? ? //完成 main 方法
? ? public static void main(String[] args) {
? ? ? ? int[]scores={89 , -23 , 64 , 91 , 119 , 52 , 73};
? ? ? ? HelloWorld hello=new HelloWorld();
? ? ? ??
? ? ? ? ? ? hello.topThree(scores);
? ? ? ??
? ? }
? ??
? ? //定義方法完成成績排序并輸出前三名的功能
? ??
? ? public void topThree(int[] scores){
? ? ? ? Arrays.sort(scores);
? ? ? ? for(int i=scores.length-1;i>=0;i--){
? ? ? ? if(scores[i]>=0 && scores[i]<=100){System.out.println("考試成績的前三名為:");
? System.out.println(scores[i-1]);
? ? System.out.println(scores[i-2]); ?
? ? ? System.out.println(scores[i-3]); ?
? ? ? ? }
? ? ? ? }
? ? ? ??
? ? ? ? }
2016-03-13
將負數和過一百的拋出 ? 就是-23和119
用if語句 ?拋出是continue ?
拋出后所有的都留下 ? 你使用的是大于0&&小于100
這樣對后面的施行方法無法進行具體編程
合適的進行輸出 ?不合適的沒寫怎么進行處理
而如果用拋出 ? 不合適的拋出了 ? 合適的下面接著處理
拋出語句是
if(scores[i]<0 || scores>100){
continue ? ?
}
for(int i=scores.length-1;i>=scores.length-2;i--){
? ? ? ? ? ?if(scores[i]>=0 && scores[i]<=100){
? ? ? ? ? ? ? ? System.out.println("考試成績的前三名為:");
? ? ? ? ? ? ? ? System.out.println(scores[i-1]);
? ? ? ? ? ? ? ?System.out.println(scores[i-2]); ?
? ? ? ? ? ? ? ? System.out.println(scores[i-3]); ?
你這樣不對 ? ? 你應該
private void score(int[] scores) { ? ? ? ? ? //定義方法
? ? ? ?Arrays.sort(scores); ? ? ? ? ? ? ? ? ? ? ? ?//遍歷數組 ? ? ? ? ? 這倆你也寫了
? ? ? ?int num=0; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//定義名次 ? ?初始化為0
? ? ? ?for(int i=scores.length-1;i>0;i--){ ? ? ? ? ? ? ? //定義i ? 對數組倒敘開始
? ? ? ? ? ? ? if (scores[i]<0 || scores[i]>100){ ? ? ? ? ?//前面說的拋出不正常數字
? ? ? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? num++; ? ? ? ? ? ? ? ? ? ? ? ? //i 減個1 ? num減個1 ? 當i 進行三個時 ? num為3 ? 前三名就確定了
? ? ? ? ? ? ? if (num>3) { ? ? ? ? ? ? ? ? ? //當num大于3后 ?不在進行 ? 斷開方法為break
? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? System.out.println(scores[i]); ? ? ? ? ? ? ? //i ?進行了3位 ? ?輸出數組
? ? ? ? ? ? ? }? ?
? ? ? ? ? ? ? ? ? 思路:題意輸出前三名,當出現數字時絕對會有相對應的常量 ? ?咱們設置的是num
? ? ? ? ? ? ? ? ? ? ? ? ? ?在對正確數字進行選擇時要先將異常數字拋出 ?就是前面說的continue
? ? ? ? ? ? ? ? ? ? ? ? ? ?遍歷數組你知道了,也知道從后向前取值,所以取值就從數組最后一位length-1開始一直到0
? ? ? ? ? ? ? ? ? ? ? ? ? 不能自己中斷要一直到0,要靠條件自動停止,條件就是num>3, ?當大于3時取值結束
? ? ? ? ? ? ? ? ? ? ? ? ? 方法返回值為scores[i],再在前面調用,這個你沒寫錯 ?就不說了
2016-03-14
最后改成了這樣,OK了。謝謝nosilence和各位的回答!
import java.util.Arrays;
public class HelloWorld {
//完成 main 方法
? ? public static void main(String[] args) {
? ? ? ? int[]scores={89 , -23 , 64 , 91 , 119 , 52 , 73};
? ? ? ? System.out.println("考試成績的前三名為:");
? ? ? ? HelloWorld hello=new HelloWorld();
? ? ? ? ? ? hello.topThree(scores); ??
? ? }
? ??
? ? //定義方法完成成績排序并輸出前三名的功能
? ??
? ? public void topThree(int[] scores){
? ? ? ? Arrays.sort(scores);
? ? ? ? int jj=0;
? ? ? ? for(int i=scores.length-1;i>=0;i--){
? ? ? ? if(scores[i]>=0 && scores[i]<=100){
? ? ? ? System.out.println(scores[i]);?
? ? ? ? int countt=++jj;
? ? ? ? ? ? if(countt>2){
? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? ?}
? }
? ? ? ?}
}
2016-03-13
你這樣寫肯定不行的。
sort排序后是升序,即從大到小,為了輸出成績最高的前三個,理論上是要輸出后面的三個,又因為數據里面有無效的數據(比如小于0;大于100),因此倒敘取后面的數據的時候,要檢驗成績是否0>=0 && <=100,符合條件的輸出,并用一個int計數器來存儲已經輸出了的有效成績個數,每次輸出就++,滿3個了就break出循環,結束。
2016-03-13
從length-1到0 ? ?為for(int i=length-1;i>0:i--)
i要大于0 ? 不能大于等于 ? 這樣當i為0時還會減1 ??
2016-03-13
有相應的變量 ? ?num是變量
2016-03-13
額,還要改
? ? ? ? ? ? ? ? System.out.println(scores[i]);
? ? ? ? ? ? ? ? System.out.println(scores[i-1]); ?
? ? ? ? ? ? ? ? System.out.println(scores[i-2]); ?
2016-03-13
import java.util.Arrays;
public class HelloWorld {
? ??
? ? //完成 main 方法
? ? public static void main(String[] args) {
? ? ? ? int[]scores={89 , -23 , 64 , 91 , 119 , 52 , 73};
? ? ? ? HelloWorld hello=new HelloWorld();
? ? ? ??
? ? ? ? ? ? hello.topThree(scores);
? ? ? ??
? ? }
? ??
? ? //定義方法完成成績排序并輸出前三名的功能
? ??
? ? public void topThree(int[] scores){
? ? ? ? Arrays.sort(scores);
/*只是將你的for終止條件改變,你的循環只需要運行兩次就得出結果了,但不具有普適性,如果前兩個都大于100就不行了。
http://www.xianlaiwan.cn/qadetail/124709 ?看下我的,還有標準答案 */
? ? ? ? for(int i=scores.length-1;i>=scores.length-2;i--){
? ? ? ? ? ?if(scores[i]>=0 && scores[i]<=100){
? ? ? ? ? ? ? ? System.out.println("考試成績的前三名為:");
? ? ? ? ? ? ? ? System.out.println(scores[i-1]);
? ? ? ? ? ? ? ? System.out.println(scores[i-2]); ?
? ? ? ? ? ? ? ? System.out.println(scores[i-3]); ?
? ? ? ? ? ? }
? ? ? ? }
? ? ? ??
? ? }
?}
2016-03-13
既然是for循環。何必要scores[i-1],scores[i-2],scores[3]呢,直接scores[i]就可以了。