import java.util.Scanner;
import java.util.Arrays;
//学生类
class Students{
float[] score = new float[5]; //定义数组存放五个学生的成绩
Scanner input = new Scanner(System.in);
//录入分数
public void inputName(){
for(int i=0; i<score.length; i++){
System.out.print("请输入第"+(i+1)+"个学生的成绩:");
score[i] = input.nextFloat();
}
}
//返回成绩数组的方法
public float[] getScore(){
for(int i=0; i<score.length; i++)
System.out.print(score[i]+"\t"); //输出排序前的成绩
System.out.println();
Arrays.sort(score); //将数组排序并返回数组
return score;
}
}
public class Dm{
public static void main(String[] args){
Students t = new Students();
t.inputName();
float[] arrayScore = new float[5]; //很奇怪,这里数组长度大于小于5都能编译运行通过。
arrayScore = t.getScore(); //调用getScore()方法得到数组,并赋给新数组arrayScore;
//float[] arrayScore = t.getScore(); //上两句还能缩写成这一句
for(int i=0; i<arrayScore.length; i++){
System.out.print(arrayScore[i]+"\t");
}
}
}
/*----------------------
28.0 13.0 78.0 92.0 69.0
13.0 28.0 69.0 78.0 92.0
----------------------*/
数组类型的方法返回值
最新推荐文章于 2024-07-13 02:42:07 发布