java loop exercises

1. Define an array of length 10 to store the names of students, and complete the following functions of the student management system based on this array: 1. Enroll in 2. Withdraw from school 3. View all students 4. Find out if there is a student 5. Exit the system. Before the user selects "5. Exit the system", the first four functions can be used repeatedly.
 – View all students
 – Admission
 – Withdrawal
 – Find out if the student exists
 – Exit the system
2. Grading rules
(1) Define an array of length 10 to store the names of class students (5 points)
(2) The menu is written correctly (10)
①The loop processing is correct (5 points)
②The branch structure is correct (5 points)
(3) Entrance function (15 points)
①Able to register successfully (5 points)
②Can not register successfully after the class is full and give corresponding prompts (5 points)
③If the class is full of 10 people, it can be registered correctly (5 points)
(4) Withdrawal function (10 points)
①Successfully withdraw from school (you can no longer find the school after you quit) (5 points)
②There is no student who wants to quit school to prompt (5 points)
(5) View all student functions (10 Points)
①There are students in the class that can be displayed correctly (only the name of the student is displayed, and the empty data is not displayed) (5 points)
②There is no student in the class with a prompt (5 points)
(6) Find whether the student has a function (10 points)
①Can find this Students give hints (5 points)
②No students can give hints correctly (5 points)
(7) Exit the system function (5 points)

public class Test1 {
    
    
	public static void main(String[] args) {
    
    	
	Scanner sc = new Scanner(System.in);
	String[] students = new String[10];
	int index = 0;//学生的下标
	out:while(true) {
    
    
		System.out.println("欢迎使用学生管理系统");
		System.out.println("1.入学 2.退学 3.查看所有学生 4.查找指定学生 5,。退出系统");
		int num = sc.nextInt();
		switch (num) {
    
    
		case 1:
			System.out.println("请输入要入学的学生姓名");
			String name = sc.next();
			//先判断 数组已经满了
			if(index<10) {
    
    
				students[index]=name;
				index++;
				System.out.println("注册成功");
			}else {
    
    
				System.out.println("学员已经满了,不能再注册了");
			}
			break;
		case 2:
			System.out.println("请输入要退学的学员");
			String name3 = sc.next();
			boolean f2 = false;
			for (int i = 0; i < index; i++) {
    
    
				if(name3.equals(students[i])) {
    
    
					f2=true;//将后面的元素覆盖前面的元素
					for (int j = 1; j < index-1; j++) {
    
    
						students[j]=students[j+1];
					}
					//总人数-1
					index--;
					System.out.println("退学成功");
					break;
				}				
			}
			if(f2==false) {
    
    
				System.out.println("查无此人");
			}
			break;
		case 3:
			for (int i = 0; i < index; i++) {
    
    
				System.out.println(students[i]);
			}
			break;
		case 4:
			System.out.println("请输入要查找的学生");
			String name2 = sc.next();
			boolean f = false;
			for (int i = 0; i < index; i++) {
    
    
				if(name2.equals(students[i])) {
    
    
					f=true;
					break;
				}				
			}
			if(f) {
    
    
				System.out.println("找到了");
			}else {
    
    
				System.out.println("不存在此学员");
			}
			break;
		case 5:
			System.out.println("退出系统");
			break out;
		default:
			System.out.println("输入错误,请重新输入");
			break;
		}		
	}
}	
	}

Guess you like

Origin blog.csdn.net/Echoxxxxx/article/details/112436908