首页 > 其他分享 >2024/9/20日 日志

2024/9/20日 日志

时间:2024-09-20 23:34:21浏览次数:7  
标签:20 String System 2024 stunumber student println 日志 out

今天,进行了新的测试。
题目为简单的学生管理系统,包含增删改查。

点击查看代码
//张一衡 20234023
import java.util.ArrayList;
import java.util.Scanner;

public class Moontest {
    public static void main(String[] args) {
        StudentManager studentManager = new StudentManager();
        studentManager.runMenu();
    }
}

class Student {
    private String stunumber;
    private String name;
    private int age;
    private boolean sex;
    private double score;

    public Student() {}

    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;
    }

    // Get and Set
    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 getSex() {
        return sex;
    }
    public void setSex(boolean sex) {
        this.sex = sex;
    }
    public double getScore() {
        return score;
    }
    public void setScore(double score) {
        this.score = score;
    }
}

class StudentManager {
    private ArrayList<Student> students = new ArrayList<>();
    private Scanner scanner = new Scanner(System.in);

    void showStudent() {
        if(students.isEmpty()) {
            System.out.println("学生信息为空!");
        }else{
            for(Student student:students) {
                System.out.println("*************************************");
                System.out.println("学号为: " + student.getStunumber());
                System.out.println("姓名为: " + student.getName());
                System.out.println("年龄为: " + student.getAge());
                System.out.println("性别为: " + (student.getSex() ? "男" : "女"));
                System.out.println("分数为: " + student.getScore());
                System.out.println("*************************************");
            }
        }
    }
    void addStudent(String stunumber, String name, int age, boolean sex, double score) {
        for (Student student : students) {
            if (student.getStunumber().equals(stunumber)) {
                System.out.println("学号重复!");
                return;
            }
        }
        students.add(new Student(stunumber, name, age, sex, score));
    }

    void deleteStudent(String stunumber) {
        for (int i = 0; i < students.size(); i++) {
            if (students.get(i).getStunumber().equals(stunumber)) {
                students.remove(i);
                System.out.println("学生信息已删除。");
                return;
            }
        }
        System.out.println("未找到该学号的学生。");
    }

    void updateStudent(String stunumber) {
        for (Student student : students) {
            if (student.getStunumber().equals(stunumber)) {
                System.out.println("请输入你要修改的学生信息" +"(学号,姓名,年龄,性别(1 = 男 / 0 = 女),成绩):");
                String newStunumber = scanner.nextLine();
                String changename = scanner.nextLine();
                int changeage = scanner.nextInt();
                boolean changesex = scanner.nextInt() == 1;
                double changescore = scanner.nextDouble();
                student.setStunumber(newStunumber);
                student.setName(changename);
                student.setAge(changeage);
                student.setSex(changesex);
                student.setScore(changescore);
                System.out.println("学生信息已更新。");
                return;
            }else{
                System.out.println("未找到该学号的学生。");
            }
        }
    }

    void selectStudent(String stunumber) {
        for (Student student : students) {
            if (student.getStunumber().equals(stunumber)) {
                System.out.println("*************************************");
                System.out.println("学号为: " + student.getStunumber());
                System.out.println("姓名为: " + student.getName());
                System.out.println("年龄为: " + student.getAge());
                System.out.println("性别为: " + (student.getSex() ? "男" : "女"));
                System.out.println("成绩为: " + student.getScore());
                System.out.println("*************************************");
                return;
            }
        }
        System.out.println("未找到该学号的学生。");
    }

    void showMenu() {
        System.out.println("***************************************************************");
        System.out.println("          石家庄铁道大学软件工程系学生信息管理系统");
        System.out.println("***************************************************************");
        System.out.println("                    1、遍历输出学生信息:");
        System.out.println("                    2、新学生信息录入:");
        System.out.println("                    3、删除学生信息:");
        System.out.println("                    4、修改学生信息:");
        System.out.println("                    5、查询学生信息:");
        System.out.println("***************************************************************");
    }

    void runMenu() {
        do {
            showMenu();
            System.out.print("请输入你的选择: ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // Consume newline
            switch (choice) {
                case 1:
                    showStudent();
                    break;
                case 2:
                    System.out.println("请输入你要添加的学生信息(学号,姓名,年龄,性别(1 = 男 / 0 = 女),成绩):");
                    String addnumber = scanner.nextLine();
                    String addname = scanner.nextLine();
                    int addage = scanner.nextInt();
                    boolean addsex = scanner.nextInt() == 1;
                    double addscore = scanner.nextDouble();
                    addStudent(addnumber, addname, addage, addsex, addscore);
                    break;
                case 3:
                    System.out.print("请输入你要删除的学生学号:");
                    String dnumber = scanner.nextLine();
                    deleteStudent(dnumber);
                    break;
                case 4:
                    System.out.println("请输入你要修改学生信息的学号:");
                    String changestunumber=scanner.nextLine();
                    updateStudent(changestunumber);
                    break;
                case 5:
                    System.out.print("请输入你要查询的学生学号:");
                    String selnumber = scanner.nextLine();
                    selectStudent(selnumber);
                    break;
                default:
                    System.out.println("该选项不存在,请你重新输入!");
            }
        } while (true);
    }
}

在代码中我运用了ArrayList来进行空间部署以处理定量空间问题,当然数组也是可以处理的。
以下是用数组的处理方式,大同小异,只是用Count计数来处理有效问题。

点击查看代码 ```

import java.util.Scanner;

public class Moontest {
public static void main(String[] args) {
StudentManager studentManager = new StudentManager();
studentManager.runMenu();
}
}

class Student {
private String stunumber;
private String name;
private int age;
private boolean sex;
private double score;

public Student() {}

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;
}

// Getters and Setters
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 getSex() {
    return sex;
}
public void setSex(boolean sex) {
    this.sex = sex;
}

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

}

class StudentManager {
private Student[] students = new Student[5];
private int studentCount = 0; // Keep track of the number of students
private Scanner scanner = new Scanner(System.in);

void showStudent() {
    if (studentCount == 0) {
        System.out.println("学生信息为空!");
        return;
    }
    for (int i = 0; i < studentCount; i++) {
        Student student = students[i];
        System.out.println("*************************************");
        System.out.println("学号为: " + student.getStunumber());
        System.out.println("姓名为: " + student.getName());
        System.out.println("年龄为: " + student.getAge());
        System.out.println("性别为: " + (student.getSex() ? "男" : "女"));
        System.out.println("分数为 + student.getScore());
        System.out.println("*************************************");
    }
}

void addStudent(String stunumber, String name, int age, boolean sex, double score) {
    if (studentCount >= students.length) {
        System.out.println("学生人数已满,无法添加更多学生!");
        return;
    }
    for (int i = 0; i < studentCount; i++) {
        if (students[i].getStunumber().equals(stunumber)) {
            System.out.println("学号重复!");
            return;
        }
    }
    students[studentCount] = new Student(stunumber, name, age, sex, score);
    studentCount++;
    System.out.println("学生信息添加成功!");
}

void deleteStudent(String stunumber) {
    for (int i = 0; i < studentCount; i++) {
        if (students[i].getStunumber().equals(stunumber)) {
            // Shift remaining elements to the left
            for (int j = i; j < studentCount - 1; j++) {
                students[j] = students[j + 1];
            }
            students[studentCount - 1] = null; // Clear the last element
            studentCount--;
            System.out.println("学生信息已删除。");
            return;
        }
    }
    System.out.println("未找到该学号的学生。");
}

void updateStudent(String stunumber, String name, int age, boolean sex, double score) {
    for (int i = 0; i < studentCount; i++) {
        if (students[i].getStunumber().equals(stunumber)) {
            students[i].setName(name);
            students[i].setAge(age);
            students[i].setSex(sex);
            students[i].setScore(score);
            System.out.println("学生信息已更新。");
            return;
        }
    }
    System.out.println("未找到该学号的学生。");
}

void selectStudent(String stunumber) {

Count; i++) {
if (students[i].getStunumber().equals(stunumber)) {
Student student = students[i];
System.out.println("");
System.out.println("学号为: " + student.getStunumber());
System.out.println("姓名为: " + student.getName());
System.out.println("年龄为: " + student.getAge());
System.out.println("性别为: " + (student.getSex() ? "男" : "女"));
System.out.println("成绩为: " + student.getScore());
System.out.println("
");
return;
}
}
System.out.println("未找到该学号的学生。");
}

void showMenu() {
    System.out.println("***************************************************************");
    System.out.println("          石家庄铁道大学软件工程系学生信息管理系统");
    System.out.println("***************************************************************");
    System.out.println("                    1、遍历输出学生信息:");
    System.out.println("                    2、新学生信息录入:");
    System.out.println("                    3、删除学生信息:");
    System.out.println("                    4、修改学生信息:");
    System.out.println("                    5、查询学生信息:");
    System.out.println("***************************************************************");
}

void runMenu() {
    do {
        showMenu();
        System.out.print("请输入你的选择: ");
        int choice = scanner.nextInt();
        scanner.nextLine(); // Consume newline
        switch (choice) {
            case 1:
                showStudent();
                break;
            case 2:
                System.out.println("请输入你要添加的学生信息(学号,姓名,年龄,性别(1 = 男 / 0 = 女),成绩):");
                String addnumber = scanner.nextLine();
                String addname = scanner.nextLine();
                int addage = scanner.nextInt();
                boolean addsex = scanner.nextInt() == 1;
                double addscore = scanner.nextDouble();
                addStudent(addnumber, addname, addage, addsex, addscore);
                break;
            case 3:
                System.out.print("请输入你要删除的学生学号:");
                String dnumber = scanner.nextLine();
                deleteStudent(dnumber);
                break;
            case 4:
                System.out.println("请输入你要修改的学生信息(学号,姓名,年龄,性别(1 = 男 / 0 = 女),成绩):");
                String changestunumber = scanner.nextLine();
                String changename = scanner.nextLine();
                int changeage = scanner.nextInt();
                boolean changesex = scanner.nextInt() == 1;
                double changescore = scanner.nextDouble();
                updateStudent(changestunumber, changename, changeage, changesex, changescore);
                break;
            case 5:
                System.out.print("请输入你要查询的学生学号:");
                String selnumber = scanner.nextLine();
                selectStudent(selnumber);
                break;
            default:
                System.out.println("该选项不存在,请你重新输入!");
        }
    } while (true);
}

}

</details>

                                                                              ----------------------Moonbeamsc.

标签:20,String,System,2024,stunumber,student,println,日志,out
From: https://www.cnblogs.com/MoonbeamsC/p/18423458

相关文章

  • 2024/9/20
    今天进行了Java测试成功通过。importjava.util.Scanner;importjava.util.Arrays;importjava.util.*;classStudent{privateStringstunumber;//学号privateStringname;//姓名privateintage;//年龄privatebooleansex;//男生为false,女生为trueprivatedoublesc......
  • 信息安全数学基础(20)中国剩余定理
    前言    信息安全数学基础中的中国剩余定理(ChineseRemainderTheorem,简称CRT),又称孙子定理,是数论中一个重要的定理,主要用于求解一次同余式组。一、背景与起源    中国剩余定理最早见于我国南北朝时期的数学著作《孙子算经》中的“物不知数”问题。该问题可......
  • 2024.9.20
    publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);StudentManagermanager=newStudentManager(5);while(true){System.out.println("");System.out.println("石家庄铁道大学软件工程系学生信息管理系统");System.out.println(&q......
  • hdu2063过山车
    1、匈牙利算法;2、二分图最大匹配importjava.util.Scanner;publicclassMain{publicstaticbooleanfind(intcur,int[]pre,boolean[][]map,boolean[]vis){for(inti=1;i<pre.length;i++){if(map[cur][i]&&!vis[i]){......
  • 二级C语言2023-9易错题
    1二叉树结点数计算:一棵二叉树有10个度为1的结点,7个度为2的结点,则该二叉树共有____个结点。解:2指针:有以下程序#inctude<stdio.h>#include<stdlib.h>main(){ int*a,*b,*c; a=b=c=(int*)malloc(sizeof(int)); *a=1;*b=2,*c=3; a=b; printf("%d,%d,%d\n",*a,*b,*c);}程序......
  • 代码随想录算法训练营第一天 | 209. 长度最小的子数组 59. 螺旋矩阵 58. 区间和 Java
    209.长度最小的子数组题目链接:https://leetcode.cn/problems/minimum-size-subarray-sum/description/解题思路思路1:暴力解法通过两个for循环,找出所有的可能的区间,并比较出最小区间思路2:滑动窗口因为需要找出的是连续的一个子数组,所以可以模拟一个从左到右滑动的一个......
  • java学习9.20
    今天是简单的java小测验,实现简单的增删改查操作。我先用数组完成。后面的话想实现连接数据库的增删改查,但是始有bug不知道咋改,写的少不清楚问题出在哪,多写几回应该就能对症下药。下面是数组的代码Student类publicclassStudent{Stringstunumber;Stringname;......
  • csp2024 游寄
    不知不觉中,学OI已经一年了啊day-\(\infty\)打了一场模拟赛喜提历史最好成绩:颓颓颓day-6做了一下去年的初赛喜提57.5(SD分数线76尸体不好了/tuday-5又是模拟赛,达到历史最差成绩:不会打表导致的(确信咋办啊有点慌。。。。。day-4开始去b站搜视频,搞初赛做了不少笔......
  • 2024/9/20日工作日志
    Java第二次测试代码:publicclassStudent{Stringstunumber;Stringname;intage;booleansex;doublescore;publicStudent(Stringstunumber,Stringname,intage,booleansex,doublescore){this.stunumber=stunumber;this.name=name;this.age......
  • 9.20~
    9.20上午晚上学校空调好像半夜就断电了(byd之前用薄被子就给我冻醒现在拿厚被子来就热死我是吧......