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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© wx_pmVIrag6 中级黑马   /  2016-8-1 23:20  /  337 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

import java.util.Scanner;
class Demo3 {
        public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);

                String[] name = new String[5];
                int[] code = new int[5];
                double[] score = new double[5];
                int[] num = new int[5];

                //输入学生信息
                for (int i = 0; i < code.length; i++) {
                        System.out.println("输入学生学号:");
                        code[i] = sc.nextInt();
                        name[i] = sc.nextLine();

                        System.out.println("输入学生姓名:");
                       
                        name[i] = sc.nextLine();

                        System.out.println("请输入学生成绩:");
                        score[i] = sc.nextDouble();
                }

                Student st = new Student(code,name,score);
               
                //输出学生信息
                st.show();

                //修改学生成绩
                int newCode = sc.nextInt();
                st.chengji(newCode);
                st.show();


                //删除学生信息
                int newCode1 = sc.nextInt();
                st.shanchu(newCode1);
                st.show();


                System.out.println("Hello World!");
        }
}

class Student {
        String[] name;
        int[] code;
        double[] score;
       
        //无参构造方法
        public Student(){}
       
        //有参构造方法
        public Student(int[] code, String[] name, double[] score){
                this.code = code;
                this.name = name;
                this.score = score;
        }
       
        //name的set/get方法
        public void setName(String[] name){
                this.name = name;
        }
        public String[] getName(){
                return this.name;
        }
       
        //code的set/get方法
        public void setCode(int[] code){
                this.code = code;       
        }
        public int[] getCode(){
                return this.code;
        }

        //score的set/get方法
        public void setScore(double[] score){
                this.score = score;
        }
        public double[] getScore(){
                return this.score;
        }
       
        //输出学生信息
        public void show(){
                System.out.println("学生信息是:");
               
                for (int i = 0; i < code.length; i++) {
                        System.out.println("学号:"+ code[i] + "\t"  +"姓名" + name[i] + "\t" +"成绩" + score[i]);
                }
        }
       
        //修改学生成绩
        public void chengji(int newCode){
                Scanner sc = new Scanner(System.in);

                for (int i = 0; i < code.length; i++) {
                        if (newCode == code[i]) {
                                System.out.println("请修改学生成绩:");
                                double a = sc.nextDouble();
                                score[i] = a;
                                break;
                        }
                }
        }
       
        //删除学生信息
        public void shanchu(int newCode){
                Scanner sc = new Scanner(System.in);

                for (int i = 0; i < code.length; i++) {
                        if (newCode == code[i]) {
                                System.out.println("请删除学生信息:");
                                code[i] = 0;
                                score[i] = 0;
                                name[i] = null;
                                break;
                        }
                }
        }
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马