A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

奋斗中 加油
第一个类
import java.util.Scanner;
class Demo{
        public static void main(String[] args){
                System.out.println("请输入"+Person.city+"有多少人:");
                Person[] p=new Person[MyMath.judge((new Scanner(System.in).next()))];//判断用户输入是否合法
                MyMath.paddingYes(p);//填充数组
                MyMath.stamp(p);//打印数组
                MyMath.stampMax(MyMath.lookupMax(p));//打印年龄最大的人
                MyMath.stampMin(MyMath.lookupMin(p));//打印年龄最小的人
                MyMath.stampAverage(p,MyMath.average(p));//打印平均年龄
        }
}
第二个类
import java.util.Scanner;
//实现类
public class MyMath{
        //私有化构造器
        private MyMath(){
        }
        static Scanner sc=new Scanner(System.in);
        //填充数组方法1--调用无参构造方法
        public static void paddingNo(Person[] p){
                for(int i=0;i<p.length;i++){
                        System.out.println("请输入第"+(i+1)+"个人的信息:");
                        Person per=new Person();//实例化一个Person对象 调用无参构造方法通过set()设置值
                        System.out.print("姓名:");
                        per.setName(sc.next());
                        System.out.print("性别:");
                        per.setSex(sc.next());
                        System.out.print("年龄:");
                        per.setAge(MyMath.judge(sc.next()));//判断用户输入是否合法
                        p[i]=per;//将每个对象的“引用”存储在数组中
                }
        }
        //填充数组方法2--调用带参数的构造方法
        public static void paddingYes(Person[] p){
                for(int i=0;i<p.length;i++){
                        System.out.println("请输入第"+(i+1)+"个人的信息:");
                        System.out.print("姓名:");
                        String name=sc.next();
                        System.out.print("性别:");
                        String sex=sc.next();
                        System.out.print("年龄:");
                        int age=MyMath.judge(sc.next());//判断用户输入是否合法
                        Person per=new Person(name,sex,age);//实例化一个Person对象 调用带参数的构造方法设置值
                        p[i]=per;//将每个对象的“引用”存储在数组中
                }
        }
        //打印数组
        public static void stamp(Person[] p){
                for(int i=0;i<p.length;i++){
                        System.out.println("城市:"+Person.city+" 姓名:"+p[i].getName()+" 性别:"+p[i].getSex()+" 年龄:"+p[i].getAge());
                }
        }
        //找出年龄最大的人
        public static Person lookupMax(Person[] p){
                Person per=p[0];
                for(int i=0;i<p.length;i++){
                        per = per.getAge()<p[i].getAge()?p[i]:per;
                }
                return per;
        }
        //找出年龄最小的的人
        public static Person lookupMin(Person[] p){
                Person per=p[0];
                for(int i=0;i<p.length;i++){
                        per=per.getAge()>p[i].getAge()?p[i]:per;
                }
                return per;
        }
        //求平均年龄
        public static int average(Person[] p){
                int sum=0;
                for(int i=0;i<p.length;i++){
                        sum+=p[i].getAge();
                }
                return sum/p.length;
        }
        //打印一个人的信息
        public static void stampOne(Person p){
                System.out.println("城市:"+Person.city+" 姓名:"+p.getName()+" 性别:"+p.getSex()+" 年龄:"+p.getAge());
        }
        //打印年龄最小的一个人的信息
        public static void stampMin(Person p){
                System.out.println(Person.city+"年龄最小的人是--姓名:"+p.getName()+" 性别:"+p.getSex()+" 年龄:"+p.getAge());
        }
        //打印年龄最大的一个人的信息
        public static void stampMax(Person p){
                System.out.println(Person.city+"年龄最大的人是--姓名:"+p.getName()+" 性别:"+p.getSex()+" 年龄:"+p.getAge());
        }
        //打印平均年龄
        public static void stampAverage(Person[] p,int age){
                System.out.println(Person.city+p.length+"个人的平均年龄为:"+age);
        }
        //判断用户输入是否合法
        public static int judge(String num){
                int n=0;
                while(true){
                        if(num.matches("[0-9]+")){//判断是否为数字0--9
                                n=Integer.parseInt(num);
                                if(n>=0&&n<=100){//判断是否在0--100之间
                                        break;
                                }
                                else{
                                        System.out.print("非法年龄,请重新输入:");
                                        num=sc.next();
                                }
                        }
                        else{
                                System.out.print("非法年龄,请重新输入:");
                                        num=sc.next();
                        }
                }
                return n;
        }

}
第三个类:
class Person{
        public static final String city="北京";//常量
        private String name;
        private String sex;
        private int age;
        Person(){
        }
        Person(String name,String sex,int age){
                this.name=name;
                this.sex=sex;
                this.age=age;
        }
        void setName(String name){
                this.name=name;
        }
        String getName(){
                return this.name;
        }
        void setSex(String sex){
                this.sex=sex;
        }
        String getSex(){
                return this.sex;
        }
        void setAge(int age){
                this.age=age;
        }
        int getAge(){
                return this.age;
        }
}


评分

参与人数 1黑马币 +10 收起 理由
zeng1994 + 10 很给力!

查看全部评分

17 个回复

正序浏览

对啊 就学了7天
回复 使用道具 举报
IT杰 中级黑马 2015-4-16 00:41:44
17#
7天学了这么多,已经很好了,加油!
回复 使用道具 举报
嗯_来吧 发表于 2015-4-16 00:25
你这帖子太有吸引力了

哪有啊。。。
回复 使用道具 举报
马士基 发表于 2015-4-16 00:08
看得哪个视频??7天学了这么多?

7天+自学了一点
回复 使用道具 举报
你这帖子太有吸引力了
回复 使用道具 举报
看得哪个视频??7天学了这么多?
回复 使用道具 举报
cody 中级黑马 2015-4-16 00:04:39
12#
7天学了这么多 楼主牛逼啊
回复 使用道具 举报
继续 努力!!!
回复 使用道具 举报
学了七天么?
回复 使用道具 举报
很好很强大
回复 使用道具 举报
继续努力
回复 使用道具 举报
你好明天 发表于 2015-4-14 10:41
7天学了这么多 楼主牛逼啊

这好容易啊。。我想考黑马   还得努力啊
回复 使用道具 举报
7天学了这么多 楼主牛逼啊
回复 使用道具 举报
加油,一起努力
回复 使用道具 举报
加油、、、、、、、、、、、、、
回复 使用道具 举报
楼主加油   一起努力  
回复 使用道具 举报
加油加油 一起学习!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马