对象
package com.fqs.demo002; public class Gril { //属性 private String name; private int age; private char sex; private String love; public Gril() { } public Gril(String name, int age, char sex, String love) { this.name = name; this.age = age; this.sex = sex; this.love = love; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public char getSex() { return sex; } public void setSex(char sex) { this.sex = sex; } public String getLove() { return love; } public void setLove(String love) { this.love = love; } }
方法
package com.fqs.demo002; public class GirlTest { public static void main(String[] args) { Gril []grils=new Gril[4]; grils[0]=new Gril("小明",18,'女',"唱歌"); grils[1]=new Gril("小蓝",59,'女',"跳舞"); grils[2]=new Gril("小红",20,'女',"rap"); grils[3]=new Gril("小白",61,'男',"篮球"); double avgAge=getAvg(grils); //获取小于平均值的个数 int countIndex=getXiao(grils,avgAge); //获取小于平均值数的下标数组 int []arrOK=getIndex(countIndex,grils,avgAge);//比如{2,3} for (int i = 0; i <countIndex ; i++) { //要打印的数组 int iindex=arrOK[i]; System.out.println("小于平均值的数组:"+grils[iindex].getName()+ grils[iindex].getAge()+grils[iindex].getSex()+grils[iindex].getLove()); } } //获取平均年龄 public static double getAvg(Gril []grils) { int sum=0; double avg=0; for (int i = 0; i < grils.length; i++) { sum=sum+grils[i].getAge(); } avg=sum/grils.length; return avg; } //获取小于平均年龄的人的个数 public static int getXiao(Gril []grils,double avgAge) { int count=0; for (int i = 0; i <grils.length; i++) { int tempAge=grils[i].getAge(); if(tempAge<avgAge){ //找到小于平均年纪的目标 count++; } } return count; } //获取小于拼环境年龄的人的下标的数组 public static int[] getIndex(int count,Gril []grils,double avgAge) { //将小于平均值的数组的下标放到一个新的数组中 int[]arr=new int[count]; boolean isFind=false;//找到小于平均值的数组了吗 返回下标; for (int i = 0; i < grils.length; i++) { int tempAge=grils[i].getAge(); if(tempAge<avgAge){ //找到小于平均年纪的女孩 并放到新数组中 isFind=true; arr[0]=i; } }return arr; } }
标签:love,String,name,sex,93,Gril,public,女朋友,平均值 From: https://www.cnblogs.com/haha1988/p/17482956.html