数组类型的方法返回值

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
----------------------*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值