憋著一泡屎寫出來的,花了一個小時,把我痛苦的呀!參考代碼
import java.util.Arrays;
public class HelloWorld {
? ??
? ? //完成 main 方法
? ? public static void main(String[] args) {
? ? ? ? HelloWorld hello = new HelloWorld();
? ? ? ? int[] arrays = new int[]{89,-23,64,91,119,52,73};
? ? ? ? hello.scoreArray(arrays);
? ? ? ??
? ? ? ??
? ? ? ??
? ? }
? ??
? ? //定義方法完成成績排序并輸出前三名的功能
? ??
? ? public int[] scoreArray(int[] arrays){
? ? ? ??
? ? ? ? Arrays.sort(arrays);
? ? ? ? int numb = 1;
? ? ? ? for(int i = arrays.length-1;i >= 0;i--){
? ? ? ? ? ? if(arrays[i]<0){
? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? }
? ? ? ? ? ? if(arrays[i]>100){
? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? }
? ? ? ? ? ? System.out.println(arrays[i]);
? ? ? ? ? ? numb++;
? ? ? ? ? ? if(numb>3){
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return arrays;
? ? }
}
2018-10-07
可以用樓上的void 不用返回值
2018-10-07
這樣的話main里邊調(diào)用的結(jié)果為數(shù)組,hello.scoreArray(arrays)這個為數(shù)組,還需要輸出數(shù)組或者逐個賦值
例如:
int num1=hello.scoreArray(arrays)[0];
int num2=hello.scoreArray(arrays)[1];
int num3=hello.scoreArray(arrays)[2];
然后輸出這三個變量
2018-10-07
scoreArray方法里再創(chuàng)建一個數(shù)組,把需要輸出的數(shù)值賦值給新的數(shù)組,然后返回值為新的數(shù)組
numb初始值改為0
例如:
?public int[] scoreArray(int[] arrays){
? ? ? ??
? ? ? ? Arrays.sort(arrays);
? ? ? ??//int numb = 1;
????????int numb=0;? ? ? //初始值為0,在0的基礎(chǔ)上做3次自加加運算,最后結(jié)果為3
????????int [] arr=new int[3];? ? ?//新的數(shù)組,因為要輸出前3名,所以長度為3
? ? ? ? for(int i = arrays.length-1;i >= 0;i--){
? ? ? ? ? ? if(arrays[i]<0){
? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? }
? ? ? ? ? ? if(arrays[i]>100){
? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? }
????????????//可以合為一句
????????????if(arrays[i]<0||arrays[i]>100){
????????????????continue;
?????????????}
? ? ? ? ? ? System.out.println(arrays[i]);??//這行可以不要
? ? ? ? ? ? numb++;
? ? ? ? ? ? if(numb>3){
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
????????? ?arr[numb-1]=arrays[i];? //這里開始逐個賦值,此循環(huán)中numb的值為1、2、3,
? ? ? ? }
? ? ? ? return arrays;
????????return arr;? ?//這里返回值為數(shù)組arr
? ? }
2018-09-29
不用返回了
2018-09-29
import java.util.Arrays;
//導(dǎo)入功能包;
public class HelloWorld?
{
? ??
? ? //完成 main 方法
? ? public static void main(String[] args)
? ? {
? ? int scores[]={89,-23,64,91,119,52,73}; ? ?
? ? HelloWorld hello = new HelloWorld(); ??
? ? hello.function(scores); ? ?
? ? }
? ??
? ? //定義方法完成成績排序并輸出前三名的功能
? ??
? ? public void function(int[] scores)
? ? {
? ? ? ? int count=0;
? ? ? ? Arrays.sort(scores);
? ? ? ?// int marks[3] ;
? ? ? ? for(int i=scores.length-1; i>=0;i--)
? ? ? ? {
? ? ? ? ? ? if(scores[i]<0|scores[i]>100){continue; }
? ? ? ? ? ? System.out.println(scores[i]);
? ? ? ? ? ? count++;
? ? ? ? ? ? if(count>=3){break;}
? ? ? ? ? ??
? ? ? ? }
? ? ? ??
? ? }
}