幫忙看看為什么錯(cuò)吧,腦子無(wú)法思考了
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);
? ? ? ??
? ? }
? ??
? ? //定義方法完成成績(jī)排序并輸出前三名的功能
? ??
? ? 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("考試成績(jī)的前三名為:");
? System.out.println(scores[i-1]);
? ? System.out.println(scores[i-2]); ?
? ? ? System.out.println(scores[i-3]); ?
? ? ? ? }
? ? ? ? }
? ? ? ??
? ? ? ? }
2016-03-13
將負(fù)數(shù)和過(guò)一百的拋出 ? 就是-23和119
用if語(yǔ)句 ?拋出是continue ?
拋出后所有的都留下 ? 你使用的是大于0&&小于100
這樣對(duì)后面的施行方法無(wú)法進(jìn)行具體編程
合適的進(jìn)行輸出 ?不合適的沒(méi)寫(xiě)怎么進(jìn)行處理
而如果用拋出 ? 不合適的拋出了 ? 合適的下面接著處理
拋出語(yǔ)句是
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("考試成績(jī)的前三名為:");
? ? ? ? ? ? ? ? System.out.println(scores[i-1]);
? ? ? ? ? ? ? ?System.out.println(scores[i-2]); ?
? ? ? ? ? ? ? ? System.out.println(scores[i-3]); ?
你這樣不對(duì) ? ? 你應(yīng)該
private void score(int[] scores) { ? ? ? ? ? //定義方法
? ? ? ?Arrays.sort(scores); ? ? ? ? ? ? ? ? ? ? ? ?//遍歷數(shù)組 ? ? ? ? ? 這倆你也寫(xiě)了
? ? ? ?int num=0; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//定義名次 ? ?初始化為0
? ? ? ?for(int i=scores.length-1;i>0;i--){ ? ? ? ? ? ? ? //定義i ? 對(duì)數(shù)組倒敘開(kāi)始
? ? ? ? ? ? ? if (scores[i]<0 || scores[i]>100){ ? ? ? ? ?//前面說(shuō)的拋出不正常數(shù)字
? ? ? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? num++; ? ? ? ? ? ? ? ? ? ? ? ? //i 減個(gè)1 ? num減個(gè)1 ? 當(dāng)i 進(jìn)行三個(gè)時(shí) ? num為3 ? 前三名就確定了
? ? ? ? ? ? ? if (num>3) { ? ? ? ? ? ? ? ? ? //當(dāng)num大于3后 ?不在進(jìn)行 ? 斷開(kāi)方法為break
? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? System.out.println(scores[i]); ? ? ? ? ? ? ? //i ?進(jìn)行了3位 ? ?輸出數(shù)組
? ? ? ? ? ? ? }? ?
? ? ? ? ? ? ? ? ? 思路:題意輸出前三名,當(dāng)出現(xiàn)數(shù)字時(shí)絕對(duì)會(huì)有相對(duì)應(yīng)的常量 ? ?咱們?cè)O(shè)置的是num
? ? ? ? ? ? ? ? ? ? ? ? ? ?在對(duì)正確數(shù)字進(jìn)行選擇時(shí)要先將異常數(shù)字拋出 ?就是前面說(shuō)的continue
? ? ? ? ? ? ? ? ? ? ? ? ? ?遍歷數(shù)組你知道了,也知道從后向前取值,所以取值就從數(shù)組最后一位length-1開(kāi)始一直到0
? ? ? ? ? ? ? ? ? ? ? ? ? 不能自己中斷要一直到0,要靠條件自動(dòng)停止,條件就是num>3, ?當(dāng)大于3時(shí)取值結(jié)束
? ? ? ? ? ? ? ? ? ? ? ? ? 方法返回值為scores[i],再在前面調(diào)用,這個(gè)你沒(méi)寫(xiě)錯(cuò) ?就不說(shuō)了
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("考試成績(jī)的前三名為:");
? ? ? ? HelloWorld hello=new HelloWorld();
? ? ? ? ? ? hello.topThree(scores); ??
? ? }
? ??
? ? //定義方法完成成績(jī)排序并輸出前三名的功能
? ??
? ? 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
你這樣寫(xiě)肯定不行的。
sort排序后是升序,即從大到小,為了輸出成績(jī)最高的前三個(gè),理論上是要輸出后面的三個(gè),又因?yàn)閿?shù)據(jù)里面有無(wú)效的數(shù)據(jù)(比如小于0;大于100),因此倒敘取后面的數(shù)據(jù)的時(shí)候,要檢驗(yàn)成績(jī)是否0>=0 && <=100,符合條件的輸出,并用一個(gè)int計(jì)數(shù)器來(lái)存儲(chǔ)已經(jīng)輸出了的有效成績(jī)個(gè)數(shù),每次輸出就++,滿3個(gè)了就break出循環(huán),結(jié)束。
2016-03-13
從length-1到0 ? ?為for(int i=length-1;i>0:i--)
i要大于0 ? 不能大于等于 ? 這樣當(dāng)i為0時(shí)還會(huì)減1 ??
2016-03-13
有相應(yīng)的變量 ? ?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);
? ? ? ??
? ? }
? ??
? ? //定義方法完成成績(jī)排序并輸出前三名的功能
? ??
? ? public void topThree(int[] scores){
? ? ? ? Arrays.sort(scores);
/*只是將你的for終止條件改變,你的循環(huán)只需要運(yùn)行兩次就得出結(jié)果了,但不具有普適性,如果前兩個(gè)都大于100就不行了。
http://idcbgp.cn/qadetail/124709 ?看下我的,還有標(biāo)準(zhǔn)答案 */
? ? ? ? for(int i=scores.length-1;i>=scores.length-2;i--){
? ? ? ? ? ?if(scores[i]>=0 && scores[i]<=100){
? ? ? ? ? ? ? ? System.out.println("考試成績(jī)的前三名為:");
? ? ? ? ? ? ? ? System.out.println(scores[i-1]);
? ? ? ? ? ? ? ? System.out.println(scores[i-2]); ?
? ? ? ? ? ? ? ? System.out.println(scores[i-3]); ?
? ? ? ? ? ? }
? ? ? ? }
? ? ? ??
? ? }
?}
2016-03-13
既然是for循環(huán)。何必要scores[i-1],scores[i-2],scores[3]呢,直接scores[i]就可以了。