初步学习的第一个小项目(老师做的)。
import java.util.Scanner;
public class ScoreManager {
// 扫描器
static Scanner input = new Scanner(System.in);
// 30个学生姓名
static String[] name = new String[30];
// 年龄
static int[] age = new int[30];
// 学号
static int[] id = new int[30];
// 学生个数
static int count = 0;
static boolean YN = false;
static String chooseYN = "Y";
public static void main(String[] args) {
while (true) {
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.print("请输入您的选择:(1、2、3、4、5)");
int choose = input.nextInt();
switch (choose) {
case 1:
select();
break;
case 2:
add();
break;
case 3:
update();
break;
case 4:
delete();
break;
default:
break;
}
System.out.println("是否继续进入主菜单");
if (!isContinue()) {
break;
}
}
}
// 查询信息方法
public static void select() {
while (true) {
System.out.println("序号" + "\t" + "姓名" + "\t" + "年龄" + "\t" + "学号");
for (int i = 0; i < count; i++) {
System.out.println((i + 1) + "\t" + name[i] + "\t" + age[i] + "\t" + id[i]);
}
System.out.println("是否继续查询学生信息?(Y/N)");
if (!isContinue()) {
break;
}
}
}
// 查询所有信息方法
public static void selectAll() {
System.out.println("序号" + "\t" + "姓名" + "\t" + "年龄" + "\t" + "学号");
for (int i = 0; i < count; i++) {
System.out.println((i + 1) + "\t" + name[i] + "\t" + age[i] + "\t" + id[i]);
}
}
// 添加信息方法
public static void add() {
while (true) {
System.out.print("请输入您的姓名:");
name[count] = input.next();
System.out.print("请输入您的年龄:");
age[count] = input.nextInt();
System.out.print("请输入您的学号:");
id[count] = input.nextInt();
count++;
selectAll();
System.out.println("是否继续添加?(Y/N)");
if (!isContinue()) {
break;
}
}
}
// 修改信息方法
public static void update() {
selectAll();
System.out.println("请输入您要修改的学生ID");
int oldID = input.nextInt();
System.out.println("请输入您的新姓名");
String newName = input.next();
for (int i = 0; i < count; i++) {
if (id[i] == oldID) {
name[i] = newName;
}
}
selectAll();
}
// 删除信息方法
public static void delete() {
while (true) {
System.out.println("请输入您要删除的学生的学号:");
int oldID = input.nextInt();
for (int i = 0; i < count; i++) {
if (id[i] == oldID) {
for (int j = i; j < count - i; j++) {
name[i] = name[j + 1];
age[i] = age[j + 1];
id[i] = id[j + 1];
}
}
}
System.out.println("删除成功!");
count--;
System.out.println("是否继续删除学生信息?");
if (!isContinue()) {
break;
}
}
}
// 是否返回方法
public static boolean isContinue() {
chooseYN = input.next();
if (chooseYN.equalsIgnoreCase("n")) {
return false;
}
return true;
}
}
整体思路:
1)首先做出菜单栏,我们可以选择用switch分支来做。
2)封装一个添加学生信息的方法,利用数组存放学生信息,用for循环循环输入。
3)封装一个查询学生信息的方法,利用for循环逐行输出学生信息。
4)封装一个修改学生信息的方法,利用学生的姓名或者学号用for循环找到对应的学生再给他重新赋值。
5)封装一个删除学生信息的方法,利用for循环找到对应的学生,然后再用for循环逐行覆盖。
基本数据类型
运算符
流程控制
方法
数组