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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© zlp19940327 中级黑马   /  2015-4-15 00:39  /  517 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

/*
        1.从控制接收学员的数量:
        2.根据这个数量,循环从控制台接收学员信息;
        3.计算平均分,和最高分、最低分;
*/
import java.util.Scanner;
class Student
{
        String stuName;
        int age;
        String sex;
        int score;
}
class Demo
{
        public static void main(String[] args)
        {
                Scanner sc = new Scanner(System.in);
                System.out.print("请输入学员数量:");
                int count = sc.nextInt();
                Student[] stuArray = new Student[count];//count = 3
               

                for(int i =0;i < stuArray.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 = sc.nextInt();//每次循环都会产生一个新的变量
                        System.out.print("分数:");
                        int score = sc.nextInt();//每次循环都会产生一个新的变量

                        //封装一个Student对象
                        //每次循环都会产生一个新的变量
                        Student stu = new Student();
                        stu.stuName = name;
                        stu.age = age;
                        stu.sex = sex;
                        stu.score = score;
                        //将stu放到数组中
                        stuArray[i] = stu;
                }
                //取出数据
                for(int i = 0 ;i <stuArray.length ;i ++){
                        Student stu = stuArray[i];//由于数组中存储的是Student类型的引用,所以,这里要用一个Student类型的变量接收
                        System.out.println("姓名:" + stu.stuName + " 性别:" + stu.sex + " 年龄:" + stu.age + " 分数:" + stu.score);
                }

                //找出最高分学员
                Student maxStu = stuArray[0];//假设第一个学员是最高分的学员;
                for(int i = 0;i < stuArray.length ;i++){
                        Student stu = stuArray[i];
                        if(stu.score > maxStu.score){
                                maxStu = stu;
                        }
                }

                System.out.println("最高分的学员信息:姓名:" + maxStu.stuName + " 性别:" + maxStu.sex +
                                                                " 年龄:" + maxStu.age + " 分数:" + maxStu.score);

                //找最低分:

               
        }
}


评分

参与人数 1黑马币 +6 收起 理由
zeng1994 + 6

查看全部评分

3 个回复

正序浏览
最低分的代码,没写上吧,扣分!哈哈!
回复 使用道具 举报
万物皆对象
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马