import java.util.*;public class Z14Zuoye1 {
public Students getMaxHeight(Students[] stu) {
Students max = stu[0]; for (int i = 0; i < stu.length; i++) {
/* 比较该数组那个学生最高 */
if (stu[i].hoist > max.max) {
max.max = stu[i].hoist; //保存身高的学生
max.diZ = i; // 第几名学生最高
}
}
return max;
} public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Students[] ho = new Students[10];// 创建对象数组 for (int i = 0; i < 3; i++) {
System.out.println("请输入第" + (i + 1) + "名学生的身高(CM):");
ho[i] = new Students(); // 实例化
ho[i].hoist = input.nextInt();
}
Students stu = new Students();
System.out.println("该班第" + stu.diZ + "名学生身高最高,为:" + stu.max);
}
}class Students {
int hoist; // 身高属性
int max ; // 最高身高的学生
int diZ ;// 最高身高的学生的位置}
public Students getMaxHeight(Students[] stu) {
Students max = stu[0]; for (int i = 0; i < stu.length; i++) {
/* 比较该数组那个学生最高 */
if (stu[i].hoist > max.max) {
max.max = stu[i].hoist; //保存身高的学生
max.diZ = i; // 第几名学生最高
}
}
return max;
} public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Students[] ho = new Students[10];// 创建对象数组 for (int i = 0; i < 3; i++) {
System.out.println("请输入第" + (i + 1) + "名学生的身高(CM):");
ho[i] = new Students(); // 实例化
ho[i].hoist = input.nextInt();
}
Students stu = new Students();
System.out.println("该班第" + stu.diZ + "名学生身高最高,为:" + stu.max);
}
}class Students {
int hoist; // 身高属性
int max ; // 最高身高的学生
int diZ ;// 最高身高的学生的位置}
你别说你的默认构造方法
public static Students getMaxHeight(Students[] stu) { Students max = null;
Students max1 = stu[0];
for (int i = 0; i < stu.length; i++) {
max = stu[i];
/* 比较该数组那个学生最高 */
if (max1.hoist < max.hoist) {
max1 = max;
max1.diZ = i+1;
}
}
if(max1.diZ==0){
max1.diZ=1;
}
return max1;
} public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Students[] ho = new Students[3];// 创建对象数组
Students stu = new Students(); for (int i = 0; i < 3; i++) {
System.out.println("请输入第" + (i + 1) + "名学生的身高(CM):");
ho[i] = new Students(); // 实例化
ho[i].hoist = input.nextInt();
}
stu = Z14Zuoye1.getMaxHeight(ho);
System.out.println("该班第" + stu.diZ + "名学生身高最高,为:" + stu.hoist);
}
}class Students {
int hoist; // 身高属性
int diZ;// 最高身高的学生的位置
}
Students stu = new Students();
System.out.println("该班第" + stu.diZ + "名学生身高最高,为:" + stu.max);
不能这样写
public static Students getMaxHeight(Students[] stu) {
Students max = stu[0]; for (int i = 0; i < stu.length; i++) {
/* 比较该数组那个学生最高 */
if (stu[i]!=null&&stu[i].hoist > max.max) {
max.max = stu[i].hoist; // 保存身高的学生
max.diZ = i+1; // 第几名学生最高
}
}
return max;
} public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Students[] ho = new Students[10];// 创建对象数组 for (int i = 0; i < 3; i++) {
System.out.println("请输入第" + (i + 1) + "名学生的身高(CM):");
ho[i] = new Students(); // 实例化
ho[i].hoist = input.nextInt();
} Students stu = Z14Zuoye1.getMaxHeight(ho);
System.out.println("该班第" + stu.diZ + "名学生身高最高,为:" + stu.max);
}
}class Students {
int hoist; // 身高属性
int max; // 最高身高的学生
int diZ;// 最高身高的学生的位置
}
5楼我改的比较大 这份代码只改了你的方法,main中只是加了方法的调用,应该来说这份代码还是比较适合你的,本人小菜鸟一枚,给位大神勿喷
/**
* 学生类
* @author liubo1314
*
*/
public class maxGao {
float height;//身高属性
}import java.util.Scanner;
/**
* 身高类
* @author liubo1314
*
*/
public class Height {
public static void main(String[] args) {
maxGao[] sheng = new maxGao[5];//长度为5的数组对象
Scanner input = new Scanner(System.in);
Height h = new Height();
for(int i =0; i<sheng.length; i++){
System.out.println("请输入第"+(i+1)+"个学生的身高:");
sheng[i] = new maxGao();//实例化
sheng[i].height = input.nextFloat();
}
maxGao MaxGao = h.getmaxGao(sheng);
System.out.println("为"+MaxGao.height);
}
/**
* 获取最大身高
* @param gao
* @return
*/
public maxGao getmaxGao(maxGao[] gao){
maxGao sheng = new maxGao();
float Max =0;//保存最大身高
int index = 0;
Max = gao[0].height;//赋给他对象数组内的第一个值
for(int i =1; i<gao.length; i++){
if(gao[i].height > Max){
Max = gao[i].height;
index = i;
}
}
sheng.height = Max;
System.out.print("该班第"+index+"位学生身高最高,");
return sheng;
}
}
import java.util.*;public class Test {
private static int maxStudentsNum = 3;
public static Students getMaxHeight(Students[] stu) {
Students max = stu[0]; for (int i = 0; i < stu.length && i < maxStudentsNum; i++) {
/* 比较该数组那个学生最高 */
if (stu[i].hoist > max.max) {
max.max = stu[i].hoist; // 保存身高的学生
max.diZ = i + 1; // 第几名学生最高
}
}
return max;
} public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Students[] ho = new Students[10];// 创建对象数组 for (int i = 0; i < maxStudentsNum; i++) {
System.out.println("请输入第" + (i + 1) + "名学生的身高(CM):");
ho[i] = new Students(); // 实例化
ho[i].hoist = input.nextInt();
} Students stu = getMaxHeight(ho);
System.out.println("该班第" + stu.diZ + "名学生身高最高,为:" + stu.max);
}
}class Students {
int hoist; // 身高属性
int max; // 最高身高的学生
int diZ;// 最高身高的学生的位置}