java学习9.20

今天是简单的java小测验,实现简单的增删改查操作。我先用数组完成。
后面的话想实现连接数据库的增删改查,但是始有bug不知道咋改,写的少不清楚问题出在哪,多写几回应该就能对症下药。
下面是数组的代码

Student类

public class Student {


    String stunumber;
    String name;
    int age;
    boolean sex;
    double score;


    public Student(String stunumber, String name, int age, boolean sex, double score) {
        this.stunumber = stunumber;
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.score = score;
    }

    public Student() {
    }

    public String getStunumber() {
        return stunumber;
    }

    public void setStunumber(String stunumber) {
        this.stunumber = stunumber;
    }

    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 boolean isSex() {
        return sex;
    }

    public void setSex(boolean sex) {
        this.sex = sex;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }
}

StudentManager


import java.util.Scanner;

public class StudentManager {
    Student student[] = new Student[999];
    int sum = 0;

    void page() {



        Scanner sc = new Scanner(System.in);
        int k = 1;
        while (k == 1) {
            System.out.println("1、遍历输出学生信息;");
            System.out.println("2、新学生信息录入;");
            System.out.println("3、删除学生信息;");
            System.out.println("4、修改学生信息;");
            System.out.println("5、查询学生信息;");
            switch (sc.nextInt()) {


                case 1: {
                    showStudnet();
                    break;

                }
                case 2: {


                    addStudent();
                    break;

                }
                case 3: {
                    deleteStudent();
                    break;

                }
                case 4: {
                    updateStudent();
                    break;
                }
                case 5: {


                    selectStudnet();
                    break;

                }
                case 6: {
                    k = 0;


                }

            }


        }
    }

    void showStudnet()
    {
        if(sum==0)
            System.out.println("目前没有学生信息");
        for(int i=0;i<sum;i++) {
            if (student[i].stunumber != null) {
                System.out.println("学号" + student[i].stunumber);
                System.out.println("姓名" + student[i].name);
                System.out.println("年龄" + student[i].age);
                if (student[i].sex)
                    System.out.println("性别 女");
                else
                    System.out.println("性别 男");
                System.out.println("成绩" + student[i].score);
            }

        }

    }
    void addStudent ()
    {
        Scanner sc = new Scanner(System.in);
        int x = 1;
        String stunumber="123";
        while (x == 1)
        {
            System.out.println("请输入学号");

            stunumber = sc.nextLine();
            x=0;
            for (int i = 0; i < sum; i++) {
                if (stunumber.equals(student[i].getStunumber())) {
                    System.out.println("请重新输入,有学号相同的学生");
                    x=1;
                    break;

                }
                else
                {
                    x=0;
                }
            }



        }

        String name;
        int age;
        boolean sex;
        double score;
        System.out.println("请输入姓名");
        name = sc.nextLine();
        System.out.println("请输入年龄");
        age = sc.nextInt();
        System.out.println("请输入性别,男生false,女生true");
        sex=sc.nextBoolean();
        System.out.println("请输入分数");
        score = sc.nextDouble();
        student[sum]=new Student();
        student[sum].setName(name);
        student[sum].setAge(age);
        student[sum].setScore(score);
        student[sum].setSex(sex);
        student[sum].setStunumber(stunumber);
        sum++;
        System.out.println("添加成功");


    }
    void deleteStudent()
    {

        Scanner sc = new Scanner(System.in);
        System.out.println("输入要删除的学号");

        String stunumber = sc.nextLine();
        int k=10;


        for (int i = 0; i < sum; i++) {
            if (stunumber.equals(student[i].getStunumber())) {
                student[i].setStunumber(null);
                System.out.println("删除成功");
                k=1;





                break;



            }

        }
        if(k==10)
            System.out.println("该学号不存在");



    }
    void updateStudent()
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入要修改的学号");
        int k=10;

        String stunumber = sc.nextLine();
        for (int i = 0; i < sum; i++) {
            if (stunumber.equals(student[i].getStunumber())) {
                String name;
                int age;
                boolean sex;
                double score;
                System.out.println("请输入姓名");
                name = sc.nextLine();
                System.out.println("请输入年龄");
                age = sc.nextInt();
                System.out.println("请输入性别,男生false,女生true");
                sex=sc.nextBoolean();
                System.out.println("请输入分数");
                score = sc.nextDouble();
                student[i].setName(name);
                student[i].setAge(age);
                student[i].setScore(score);
                student[i].setSex(sex);
                student[i].setStunumber(stunumber);
                System.out.println("修改成功");
                k=1;
                break;




            }


        }

        if(k!=1)
            System.out.println("该学号不存在");



    }
    void selectStudnet()
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入要查询的学号");
        int k=10;

        String stunumber = sc.nextLine();
        for (int i = 0; i < sum; i++) {
            if (stunumber.equals(student[i].getStunumber())) {
                if (student[i] != null) {
                    System.out.println("学号" + student[i].stunumber);
                    System.out.println("姓名" + student[i].name);
                    System.out.println("年龄" + student[i].age);
                    if (student[i].sex)
                        System.out.println("性别 女");
                    else
                        System.out.println("性别 男");
                    System.out.println("成绩" + student[i].score);
                    k=1;
                }


            }

        }
        if(k!=1)
            System.out.println("该学号不存在");


    }







}

main函数

public class Main {
    public static void main(String[] args) {
        StudentManager manager = new StudentManager();
        manager.page();

    }
}

posted @ 2024-09-20 21:49  臧博涛  阅读(7)  评论(0编辑  收藏  举报