package StudentMessage;
import java.util.Scanner;
public class StudentMessage_1 {
public static void main(String[] args) {
Menu mu = new Menu();
mu.Loginmenu();
mu.showLoginmenu();
}
}
class Menu{
String [] Name = new String[100];
Scanner sc =new Scanner(System.in);
public void Loginmenu(){
System.out.println(“欢迎来到学生管理系统”);
System.out.println(“");
System.out.println(“添加学生信息”);
System.out.println(“删除学生信息”);
System.out.println(“查询学生信息”);
System.out.println(“修改学生信息”);
System.out.println("退出");
System.out.println("**”);
System.out.println(“请选择您要执行的操作(按0退出):”);
}
public void showLoginmenu(){
int a = sc.nextInt();
switch(a){
case 1:
addMessage();
break;
case 2:
deleteMessage();
break;
case 3:
seachMessage();
break;
case 4:
reviseMessage();
break;
case 0:
System.out.println(“您已经成功退出”);
break;
default:
System.out.println(“输入错误,重新输入”);
break;
}
}
private void reviseMessage() {
System.out.println(“请选择你要修改的学生名:”);
String a=sc.next();
int b = punduan2(a);
if(b == -1){
System.out.println(“未找到该学生”);
}else{
for(int i=b;i<Name.length-1;i++){
if(Name[i]==null){
Loginmenu();
showLoginmenu();
return;
}
System.out.println(“请输入你要修改的学生名:”);
String c=sc.next();
Name[i]=c;
Loginmenu();
showLoginmenu();
return;
}
}
Loginmenu();
showLoginmenu();
return;
}
//添加学生信息
public void addMessage(){
int i =0;
while(i<Name.length){
System.out.println("请输入学员姓名:");
String name = sc.next();
int a = punduan(name);
if(a==-1){
System.out.println("没有重复的名字,可添加");
}else{
System.out.println("名字重复,添加失败");
break;
}
Name[i]=name;
System.out.println("添加成功,是否继续输入(y/n)");
String b = sc.next();
if(b.equals("y")){
i++;
}else if(b.equals("n")){
Loginmenu();
showLoginmenu();
break;
}else{
System.out.println("输入错误,请重新输入");
}
if(i==Name.length){
System.out.println("储存已满,请重新选择操作");
Loginmenu();
showLoginmenu();
break;
}
}
}
//判断学生信息是否已存在
private int punduan(String name) {
for(int i=0;i<Name.length;i++){
if(name.equals(Name[i])){
return i ;
}
}
return -1;
}
//删除学生信息
public void deleteMessage(){
System.out.println("请选择你要删除的学生名:");
String a=sc.next();
int b = punduan2(a);
if(b == -1){
System.out.println("未找到该学生");
}else{
for(int i=b;i<Name.length-1;i++){
if(Name[i]==null){
Loginmenu();
showLoginmenu();
return;
}
Name[i]=Name[i+1];
}
}
Loginmenu();
showLoginmenu();
return;
}
//判断名字是否找到
private int punduan2(String a) {
for(int i=0;i<Name.length;i++){
if(a.equals(Name[i])){
return i ;
}
if(Name[i] == null){
return -1;
}
}
return -1;
}
//查找学生信息
public void seachMessage(){
for(String name : Name){
if(name==null){
Loginmenu();
showLoginmenu();
return;
}
System.out.println(name);
}
}
}