JAVA之参数传递实例一

来源:互联网 发布:n卡优化守望先锋 编辑:程序博客网 时间:2024/06/04 18:04
 1、JAVA参数传递之某班第几名学生的身高最高。
1>打开记事本,写如下一段代码:
//学号属性
保存为Students.java【同上节,我还是把他放在了D盘下InputTest文件夹内】 

2>打开记事本,写如下一段代码:
import java.util.Scanner;public class Height {    /**     * @param args     */    public Students getMaxHeight(Students[] stu){        Students maxHeight= new Students();        for(int i=0;i<stu.length ;i++){            if(stu[i].height >maxHeight.height ){                maxHeight.height=stu[i].height;                maxHeight.ID =i+1 ;            }        }        return maxHeight;    }        public static void main(String[] args) {        Students[] s=new Students[10];//定义10名学生        Students maxs=new Students();        Height h=new Height();        Scanner input = new Scanner(System.in);        for(int i=0;i<s.length ;i++){            System.out.print("请输入第"+(i+1)+"个学生的身高:");            s[i]=new Students();            s[i].height=input.nextFloat();            s[i].ID=i+1;        }    maxs=h.getMaxHeight(s);    System.out.print("该班第"+maxs.ID+"名学生身高最高,为"+maxs.height);    }}
保存为Height.java【同上节,我还是把他放在了D盘下InputTest文件夹内】 
打开dos运行窗口,输入d:转到D盘,接下来输入cd InputTest转到InputTest目录下:
输入Javac Height.java 回车 java Height。

测试结果如下图:


1 0