Java-第十四章-代參的方法(二)-編程實現,輸入班裏10名學生的身高,獲得身高最高的學生要求對象數組類型方法

package com.ww.yzpA;

public class Students {
	int No;
	int Height;
}


package com.ww.yzpA;

public class Height {
	public Students getMaxHeigth(Students[] str) {
		Students A = new Students();
		for (int i = 0; i < str.length; i++) {
			if (str[i].Height > A.Height) {
				A.Height = str[i].Height;
				A.No = str[i].No;
			}
		}

		return A;

	}
}


package com.ww.yzpA;

import java.util.Scanner;

public class Text {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in = new Scanner (System.in);
		Students [] stu = new Students[10];
		Height news=new Height();
		Students Stu=new Students();
		for (int i = 0; i < stu.length; i++) {
			System.out.println("請輸入第"+(i+1)+"位學員的身高:");
			stu[i]= new Students();
			stu[i].Height=in.nextInt();
			stu[i].No=i+1;
		}
		Stu=news.getMaxHeigth(stu);		
	System.out.print("該班第"+Stu.No+"名學生身高最高,爲"+Stu.Height);
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章