亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

java第一季最后一章節 ,方法中的返回值類型為什么不可以用int[]

java第一季最后一章節 ,方法中的返回值類型為什么不可以用int[]

謝兵 2016-03-06 16:45:36
package xunhuan;import java.util.Arrays;public class qiansanm {??? //完成 main 方法??? public static void main(String[] args) {?????? qiansanm hello=new qiansanm();??????? //定義有效前三名的變量?????? System.out.println("請輸入前三名的學員成績");??????? int[] scores={89,-23,64,91,119,52,73};??????? Arrays.sort(scores);??????? hello.nums(scores); ???? }??? //定義方法完成成績排序并輸出前三名的功能??? public int[] nums(int[] scores){?? ??? ? int one = 0;???????? for(int i=0; i<scores.length;i++){?????? ??? ???? ???????????? if(scores[i]<0||scores[i]>100){??????????????? continue;?? ???????????? }??????????? one++;??????????? if(one>=3){?????????? ??? ?System.out.println( scores[i]);??????????? }?????????? return score;??????? }??? }???
查看完整描述

1 回答

?
一條小咸魚

TA貢獻457條經驗 獲得超255個贊

????public?int[]?nums(int[]?scores)?{
????????int?one?=?0;
????????for?(int?i?=?0;?i?<?scores.length;?i++)?{

????????????if?(scores[i]?<?0?||?scores[i]?>?100)?{
????????????????continue;
????????????}
????????????one++;
????????????if?(one?>=?3)?{
????????????????System.out.println(scores[i]);
????????????}
????????}
????????return?scores;
????}

其實你這個程序邏輯有問題,你隨意加一個0-100的數,就知道不對了,應該反向輸出

這是我寫的,你可以參考下

import?java.util.Arrays;

public?class?HelloWorld?{


????//?完成?main?方法

	public?static?void?main(String[]?args)?{

		int[]?scores?=?{?89,?-23,?64,?119,?52,?73?};

		HelloWorld?hello?=?new?HelloWorld();

		hello.scoSortAndPrint(scores);


	}


	//?定義方法完成成績排序并輸出前三名的功能

	public?void?scoSortAndPrint(int[]?score)?{

		Arrays.sort(score);

		int?count?=?0;

		System.out.println("考試成績的前三名為:?");

		for?(int?i?=?score.length?-?1;?i?>=?0?&&?count?<3;?i--)?{

			if?(score[i]?<=?100?&&?score[i]?>=?0)?{

				System.out.println(score[i]);

				count++;


			}

		}

	}

}


查看完整回答
反對 回復 2016-03-06
  • 1 回答
  • 0 關注
  • 1391 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號