不理解,求各位大神道友解答,謝謝謝謝謝.........
package jin;
import java.util.Arrays;
?
public class HelloWorld1{
?public static void main(String[] args){
? ? ?HelloWorld1 hello = new HelloWorld1();
? ? ? ? ? int[] scores = {89,-23,64,91,119,52,73};
? ? ? ? ? Arrays.sort(scores);
? ? ? ? ? int[] newArrays = hello.getnewArrays(scores);
? ? ? ? ? ? ?System.out.println(Arrays.toString(newArrays));
}
? ? ? ? ??
?public int[] getnewArrays(int[] scores){
?for(int i = 0; i<scores.length; i++){
?if(scores[i]>100 || scores[i]<0){
?continue;
? ? ?}
? ? ?int[] newArrays= {scores[i]};
?// ? System.out.println(Arrays.toString(newArrays));
? ?
?}
? ? ? return newArrays; //(這里為什么不能返回數(shù)組?????)
? ? ? ? ? ? ? ? ? ? ? ? //(而在方法中設(shè)定無返回值,直接輸出newArrays就可以! 求解,謝謝謝?。。?!)
?
?}
?
??
}
2016-01-26
??? public static void main(String[] args) {
??????? HelloWorld hello = new HelloWorld();
?????? ?
??????? int[] scores =new int[]{89,-23,64,91,119,52,73};
?????? ?
??????? System.out.println("考試成績的前三名為:");
??????? hello.print(scores);?????? ?
??? }
?? ?
??? //定義方法完成成績排序并輸出前三名的功能
??? public void print(int[] scores){
??????? //統(tǒng)計有效成績個數(shù)
??????? int count = 0;
?????? ?
??????? //先對數(shù)組進行排序(升序)
??????? Arrays.sort(scores);
?????? ?
??????? //輸出前三名(由于是升序,所以需要倒序遍歷)
??????? for(int i=scores.length-1;i>=0;i--){
??????????? if(scores[i]<0 || scores[i]>100){
??????????????? //如果成績不在0-100范圍之內(nèi),忽略結(jié)此次成績,束本次循環(huán),接下一次循環(huán),否則繼續(xù)
??????????????? continue;
??????????? }???
??????? //有效,個數(shù)加1
??????? count++;
?????? ?
??????? //輸出成績
??????? System.out.println(scores[i]);
?????? ?
??????? //如果成績個數(shù)大于三了,結(jié)束循環(huán)
??????? if(count>=3){
??????????? break;
??????? }
?????????? ?
??????? }
??? }
2016-01-26
import java.util.Arrarys;
public class HelloWorld {
? ??
? ? //完成 main 方法
? ? public static void main(String[] args) {
? ? ? ? int [] scores=new int[] {89,-23,64,91,199,52,73}
? ? ? ? HelloWorld score = new HelloWorld();
? ? ? ? ? score.fa(scores);
? ? ? ??
? ? ? ??
? ? }
? ??
? ? //定義方法完成成績排序并輸出前三名的功能
? ??
? ? public void fa (int [] scores){
? ? ? ? int k;
? ? ? ? Arrares.sort(scores);
? ? ? ? System.out.println("考試成績的前三名為");
? ? ? ? for(int i=0; i<scores.length;i++){
? ? ? ? ? ? if(scores[i]>=0 && scores[i]<=100 ?){
? ? ? ? ? ? ? ?System.out.println(scores[i]);
? ? ? ? ? ? ? ?k++;
? ? ? ? ? ? ? ?if(k==2){
? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ?}
? ? ? ? ? ? }
? ? ? ? ? ??
? ? ? ? }
? ? ? ??
? ? }
? ??
? ??
? ? 看看我寫的超簡單
2016-01-26
因為你的數(shù)組創(chuàng)建在循環(huán)里了= =也就是你每循環(huán)一次,都創(chuàng)建一次= =你把他放到for前面創(chuàng)建就行了。不過你整個代碼倒錯都是錯,拿出去結(jié)果也是錯的