今天进行了Java补测,题目比较简单,得了满分
代码如下
package com.exam;
//*班级:信2305-1
// 学号:20234102
// 姓名:孙艺伟 */
import java.util.Scanner;
class Student {
private String stunumber; //存储学生的学号(有8位数字组成)
private String name; //存储学生的姓名
private int age; //存储学生的年龄
private boolean sex; //存储学生的性别,男生为false,女生为true
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;
}
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;
}
}
class StudentManager{
Scanner scanner = new Scanner(System.in);
private Student[] arr = new Student[5];
private int iCount = 0;
public void showMenu() {
while (true) {
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("***********************************************************");
System.out.println("请输入选项:");
int iChoice = scanner.nextInt();
switch (iChoice) {
case 1:
showStudent(); //打印输出所有的学生信息
break;
case 2:
addStudent(); //增加一名新学生的信息,需要判断学号是否重复,如果重复提示用户,并禁止增加新学生信息
break;
case 3:
deleteStudent(); //删除一名学生信息(将该学生所有信息清空)
break;
case 4:
updateStudent(); //修改学生信息
break;
case 5:
selectStudent(); //查询并且输出该名学生的全部信息
break;
default:
System.out.println("该选项不存在,请重新输入。");
break;
}
}
}
public void showStudent() //遍历输出
{
for (int i = 0; i < iCount; i++) {
Student stu = arr[i];
System.out.println((i + 1) + ".学号:" + stu.getStunumber() +
" 姓名:" + stu.getName() +
" 年龄:" + stu.getAge() +
" 性别:" + stu.isSex() +
" 成绩:" + stu.getScore());
}
}
public void addStudent() //添加学生信息
{
System.out.print("学号:");
String number = scanner.next();
for (int i = 0; i < iCount; i++) {
if (arr[i].getStunumber().equals(number)) {
System.out.println("学号重复,无法添加学生信息。");
return;
}
}
System.out.print("姓名:");
String name = scanner.next();
System.out.print("年龄:");
int age = scanner.nextInt();
scanner.nextLine();
System.out.print("性别(男:false,女:true):");
boolean sex = scanner.nextBoolean();
System.out.print("成绩:");
double score = scanner.nextDouble();
scanner.nextLine();
if (iCount < arr.length) {
arr[iCount++] = new Student(number, name, age, sex, score);
System.out.println("学生信息添加成功。");
} else {
System.out.println("学生信息已满,无法添加新学生。");
}
}
public void deleteStudent() //删除学生信息
{
System.out.print("输入想要删除的学生学号");
String stunumber = scanner.next();
for (int i = 0; i < iCount; i++) {
if (arr[i].getStunumber().equals(stunumber)) {
arr[i] = null; // 清空学生信息
System.arraycopy(arr, i + 1, arr, i, iCount - i - 1); // 移动数组
arr[--iCount] = null; // 末尾置空
System.out.println("学生信息删除成功。");
return;
}
}
System.out.println("未找到该学号的学生信息。");
}
public void updateStudent() //修改学生信息
{
System.out.print("请输入要修改的学号: ");
String stunumber = scanner.next();
for (int i = 0; i < iCount; i++) {
if (arr[i].getStunumber().equals(stunumber) == false) {
System.out.println("学号不存在,无法修改学生信息。");
return;
}
}
System.out.print("请输入新姓名: ");
String name = scanner.next();
System.out.print("请输入新年龄: ");
int age = scanner.nextInt();
System.out.print("请输入新性别(男:false,女:true): ");
boolean sex = scanner.nextBoolean();
System.out.print("请输入新成绩: ");
double score = scanner.nextDouble();
for (int i = 0; i < iCount; i++) {
if (arr[i].getStunumber().equals(stunumber)) {
arr[i].setName(name);
arr[i].setAge(age);
arr[i].setSex(sex);
arr[i].setScore(score);
System.out.println("学生信息修改成功。");
return;
}
}
System.out.println("未找到该学号的学生信息。");
}
public void selectStudent() //查询学生信息
{
System.out.print("请输入要查询的学号: ");
String stunumber = scanner.next();
for (int i = 0; i < iCount; i++) {
if (arr[i].getStunumber().equals(stunumber)) {
System.out.println("学号:" + arr[i].getStunumber() +
" 姓名:" + arr[i].getName() +
" 年龄:" + arr[i].getAge() +
" 性别:" + arr[i].isSex() +
" 成绩:" + arr[i].getScore());
return;
}
}
System.out.println("未找到该学号的学生信息。");
}
}
class Main {
public static void main(String[] args) {
StudentManager s = new StudentManager();
s.showMenu();
}
}
标签:总结,arr,20,System,学生,println,public,out
From: https://www.cnblogs.com/Wei-n/p/18424307