如果方法既包含參數(shù)酗昼,又帶有返回值廊谓,我們稱為帶參帶返回值的方法。
例如:下面的代碼麻削,定義了一個 show 方法蒸痹,帶有一個參數(shù) name ,方法執(zhí)行后返回一個 String 類型的結(jié)果
image.png
調(diào)用帶參帶返回值的方法:
image.png
案例:將考試成績排序并輸出呛哟,返回成績的個數(shù)
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
HelloWorld hello = new HelloWorld();
int[] scores={79,52,98,81};
//調(diào)用方法叠荠,傳入成績數(shù)組,并獲取成績的個數(shù)
int count=hello.sort(scores);
System.out.println("共有"+count+"個成績信息扫责!");
}
/*
* 功能:將考試成績排序并輸出榛鼎,返回成績的個數(shù)
* 定義一個包含整型數(shù)組參數(shù)的方法,傳入成績數(shù)組
* 使用Arrays類對成績數(shù)組進行排序并輸出
* 方法執(zhí)行后返回數(shù)組中元素的個數(shù)
*/
public int sort(int[] scores){
Arrays.sort(scores);
System.out.println(Arrays.toString(scores));
return scores.length;
}
}