类和对象(1)

1、概念

//对象是类的具体(实例,个体,具体东西)

//类是对象的共性(抽象、泛指)

2、练习题

①基本练习——英雄的购买及添加

//主函数

import java.util.Scanner;

public class Heromain {
    public static void main(String[] args) {
        Hero[] hero = new Hero[5];//产生对象数组
        Scanner input = new Scanner(System.in);
        for (int i = 0; i < hero.length; i++) {
            Hero he = new Hero();//每个循环产生一个新的对象
            System.out.println("请输入第" + (i + 1) + "个英雄姓名");
            he.name = input.next();
            System.out.println("请输入第" + (i + 1) + "个英雄性别");
            he.sex = input.next();
            System.out.println("请输入第" + (i + 1) + "个英雄价格");
            he.money = input.nextInt();
            hero[i] = he;
        }
        boolean flag = false;
        int a = 0;
        Hero[] havehero = new Hero[5];
        do {
            System.out.println("-------------商城在售英雄有-----------");
            for (int x = 0; x < hero.length; x++) {//循环遍历展示
                if (hero[x]!=null) {
                    hero[x].heroshow();
                }
            }
            System.out.println("选择你要购买的英雄");
            Hero hero1 = new Hero();
            int c = hero1.heropurchase(hero);
            int i=0;
            for (i = 0; i < hero.length; i++) {//购买英雄
                if (c == i) {
                    System.out.println("购买成功");
                    Hero he1 = new Hero();//新建一个对象,用来存储购买信息
                    he1.name = hero[i].name;
                    he1.sex = hero[i].sex;
                    he1.money = hero[i].money;
                    havehero[a] = he1;//将信息存储到havehero数组中
                    hero[i] = null;
                    a++;
                    break;
                }
            }
            if (i == hero.length)
            {
                System.out.println("英雄不存在,购买失败");
            }
            //显示已拥有英雄
            System.out.println("已拥有英雄:");
            for (i = 0; i < havehero.length; i++) {
                if (havehero[i] != null) {
                    havehero[i].heroshow();//调用展示函数
                }
            }
            if (havehero[1] != null) {
                flag = true;
            }
        } while (!flag);

    }
}

//类和方法

import java.util.Scanner;

public class Hero {
    //英雄名字
    String name;
    //英雄性别
    String sex;
    //英雄价格
    int money;

    public void heroshow() {
        System.out.println(name + "\t\t\t" + sex + "\t\t\t" + money);
    }

    public int heropurchase(Hero[] a) {
        Scanner input = new Scanner(System.in);
        String str = input.next();
        int i = 0;
        for ( i = 0; i < a.length; i++) {
            if (a[i] != null && str.equals(a[i].name) ) {
                return i;
            }
        }
        return -1;
    }

}

//引用类型赋值的是地址

//基础类型赋值是将值拷贝

②课后练习——基本学生管理系统

//主函数

import java.util.Scanner;

public class StudentSystem {
    public static void main(String[] args) {
        System.out.println("------------------------------");
        System.out.println("------欢迎使用学生管理系统------");
        System.out.println("------------------------------");
        System.out.println("1、添加学生\t2、删除学生\t3、查找学生\t4、修改学生信息\t" +
                "5、打印学生信息\t6、退出");
        Scanner input = new Scanner(System.in);
        Student[] stu = new Student[5];
        Student student = new Student();
        do {
            System.out.println("请选择你需要的操作:");
            int xz = input.nextInt();
            switch (xz) {
                case 1:
                    System.out.println("请输入学生的学号");
                    int stuNo = input.nextInt();
                    System.out.println("请输入学生的姓名");
                    String stuName = input.next();
                    System.out.println("请输入学生的性别");
                    String stuSex = input.next();
                    System.out.println("请输入学生的成绩");
                    double stuScore = input.nextDouble();
                    student.addstudent(stu, stuNo, stuName, stuSex, stuScore);
                    break;
                case 2:
                    System.out.println("请输入想要删除学生的学号:");
                    int delNo = input.nextInt();
                    new Student().delete(stu, delNo);
                    break;
                case 3:
                    System.out.println("请输入查询类型");
                    System.out.println("1、学号查找\t2、姓名查找");
                    int xz1 = input.nextInt();
                    switch (xz1){
                        case 1:
                            System.out.println("请输入想要查询的学生学号");
                            int xh = input.nextInt();
                            new Student().xhcz(stu,xh);
                            break;
                        case 2:
                            System.out.println("请输入想要查询的学生姓名");
                            String xm = input.next();
                            new Student().xmcz(stu,xm);
                            break;
                    }
                    break;
                case 4:
                    System.out.println("请输入想修改的学生的学号");
                    int renum = input.nextInt();
                    new Student().replace(stu,renum);
                    break;
                case 5:
                    System.out.println("学号\t\t姓名\t\t性别\t\t成绩");
                    for (int i = 0; i < stu.length; i++) {
                        if (stu[i] != null) {
                            stu[i].show(stu);
                        }
                    }
                    break;
                case 6:
                    System.exit(0);
            }
        } while (true);
    }
}

//类和方法

import java.util.Scanner;

public class Student {
    int stuNo;
    String stuName;
    String stuSex;
    double stuScore;

    public void addstudent(Student[] a, int stuNo, String stuName, String stuSex, double stuScore) {
        int j;
        boolean flag = false;
        Student stu = new Student();//新建学生对象
        stu.stuNo = stuNo;
        stu.stuName = stuName;
        stu.stuSex = stuSex;
        stu.stuScore = stuScore;
        for (int i = 0; i < a.length; i++) {
            for (j = 0; j < a.length; j++) {
                if (a[j] != null  && stu.stuNo == a[j].stuNo) {//比较当前录入信息与数组中其他信息是否一致
                    System.out.println("该学生已存在,添加失败");
                    break;
                }
            }
            if (j == a.length) {//没有重复的人
                for (int k = 0; k < a.length; k++) {
                    if (a[k] == null ) {
                        Student stu1 = new Student();
                        stu1.stuNo = stuNo;
                        stu1.stuName = stuName;
                        stu1.stuSex = stuSex;
                        stu1.stuScore = stuScore;
                        a[k] = stu1;//将对象信息赋值到数组中
                        System.out.println("添加成功");
                        flag = true;
                        System.out.println(k);
                        break;
                    }
                }
            }
            if (flag){
                break;
            }
        }
    }

    public  void  delete(Student[] array,int a){
        boolean flag1 = false;
        for (int i = 0; i < array.length; i++) {
            Student stu2 = new Student();
            if (array[i] != null) {
                stu2.stuNo = array[i].stuNo;
                if (stu2.stuNo == a) {
                    flag1 = true;
                    System.out.println("有这个学生可以删除");
                    array[i] = null;
                    System.out.println("删除成功");
                }
            }
        }
        if (!flag1){
            System.out.println("没有这个学生,删除失败");
        }
    }

    public void xhcz(Student[] a,int xuehao){
        boolean flag = false;
        for (int i = 0; i < a.length; i++) {
            if (a[i] != null && xuehao == a[i].stuNo){
                System.out.println("找到该学生");
                flag = true;
                System.out.println(a[i].stuNo + "\t" +a[i].stuName + "\t" +a[i].stuSex+"\t" +a[i].stuScore);
                break;
            }
        }
        if (!flag){
            System.out.println("未查询到该学生,查询失败,请重新查询");
        }
    }

    public void xmcz(Student[] a,String xingming){
        boolean flag = false;
        for (int i = 0; i < a.length; i++) {
            if (a[i] != null && xingming.equals(a[i].stuName)){
                System.out.println("找到该学生");
                flag = true;
                System.out.println(a[i].stuNo + "\t" +a[i].stuName + "\t" +a[i].stuSex+"\t" +a[i].stuScore);
                break;
            }
        }
        if (!flag){
            System.out.println("未查询到该学生,查询失败,请重新查询");
        }
    }

    public void replace(Student[] a,int xuehao){
        Scanner input = new Scanner(System.in);
        boolean flag = false;
        for (int i = 0; i < a.length; i++) {
            if (a[i] != null && xuehao == a[i].stuNo){
                System.out.println("找到信息如下");
                System.out.println(a[i].stuNo + "\t" +a[i].stuName + "\t" +a[i].stuSex+"\t" +a[i].stuScore);
                System.out.println("请输入想修改的信息:");
                System.out.println("1、学号\t2、姓名\t3、性别\t4、成绩");
                int xz = input.nextInt();
                switch (xz){
                    case 1:
                        System.out.println("请输入修改后的学号:");
                        int newNo = input.nextInt();
                        a[i].stuNo = newNo;
                        System.out.println("修改成功");
                        flag = true;
                        break;
                    case 2:
                        System.out.println("请输入修改后的姓名:");
                        String newname = input.next();
                        a[i].stuName = newname;
                        System.out.println("修改成功");
                        flag = true;
                        break;
                    case 3:
                        System.out.println("请输入修改后的性别:");
                        String newSex = input.next();
                        a[i].stuSex = newSex;
                        System.out.println("修改成功");
                        flag = true;
                        break;
                    case 4:
                        System.out.println("请输入修改后的成绩:");
                        double newScore = input.nextDouble();
                        a[i].stuScore = newScore;
                        System.out.println("修改成功");
                        flag = true;
                        break;
                }
                if (!flag){
                    System.out.println("未找到该学生");
                }
            }
        }
    }

    public void show(Student[] a) {
        String str = stuNo + "\t\t" +stuName+"\t\t"+stuSex +"\t\t" +stuScore;
        for (int i = 0; i < a.length; i++) {
            if (a[i] != null) {
                System.out.println(str);
                break;
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值