Project description: Log in through the administrator account to manage student information. It can increase, modify, delete and query student information.
Knowledge points: array, do{}while loop, for loop, if statement, switch condition statement
Flowchart of student management system
import java.util.Scanner; public class Stu{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); boolean flag = false; int stuNum = 0; String[] stuName = new String[20]; String[] stuId = new String[20]; System.out.println("*********************************************"); System.out.println("* *"); System.out.println("* *"); System.out.println("* Welcome to Derun Technology Academic Affairs Management System*"); System.out.println("* *"); System.out.println("* *"); System.out.println("*********************************************"); System.out.println("/n"); do{ System.out.println("***************************************"); System.out.println("Please enter the action you want to perform"); System.out.println("1.Login the system 2.Login the system"); String num1 = sc.next(); if(num1.equals("1")){ flag = false; break; }if(num1.equals("2")){ System.exit(0); flag = false; }else{ System.out.println("Please re-enter if there is an error in input"); flag = true; } }while(flag); do{ System.out.println("Please enter login account"); String username = sc.next(); System.out.println("Please enter password"); String password = sc.next(); if(username.equals("tiantian")&&password.equals("dahai")){ System.out.println("Welcome"+username); flag = false; }else{ System.out.println("Account or password is incorrect, please log in again"); flag = true; } }while(flag); do{ System.out.println("*********************"); System.out.println("Please enter the operation you want to perform"); System.out.println("************************************************************************************************************************************************); System.out.println("1. Add student information 2. Delete student information 3. Modify student name 4. View student information 5. Exit the system"); System.out.println("******************************************************************************************************************************); String num2 = sc.next(); switch(num2){ case "1": System.out.println("Please enter the name of the student to be added"); stuName[stuNum] = sc.next(); System.out.println("Please enter the student number of the student to be added"); stuId[stuNum] = sc.next(); stuNum++; System.out.println("Add successfully"); flag = true; break; case "2": System.out.println("Please enter the student number of the student you want to delete"); String stuIdDel = sc.next(); for(int i=0;i<stuName.length;i++){ if(stuIdDel.equals(stuId[i])){ stuId[i] = null; stuName[i] = null; }else{ System.out.println("No student number of this student"); } } flag = true; break; case "3": System.out.println("Please enter the student number of the student you want to modify"); String stuIdUp = sc.next(); System.out.println("Please enter the new student name"); String stuNameUp = sc.next(); for(int i=0;i<stuId.length;i++){ if(stuIdUp.equals(stuId[i])){ stuName[i] = stuNameUp; }else{ System.out.println("No student number of this student"); } } flag = true; break; case "4": for(int i=0;i<stuName.length;i++){ if(stuName[i]==null){ continue; } System.out.println("The student's name is "+stuName[i]+"/t The student's student number is "+stuId[i]); } flag = true; break; case "5": System.exit(0); default: System.out.println("If the input is incorrect, please re-enter!!!"); flag = true; } }while(flag); } }The following is the optimized code
import java.util.Scanner; public class Stu{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); boolean flag = false; boolean ss = true; boolean aa = true; boolean bb = true; boolean cc = true; int stuNum = 0; String[] stuName = new String[20]; String[] stuId = new String[20]; String[] userNames = new String[5]; String[] passwords = new String[5]; System.out.println("***************************************************************************"); System.out.println("* *"); System.out.println("* *"); System.out.println("* *"); System.out.println("* *"); System.out.println("* *"); System.out.println("************************************************************************"); System.out.println("/n"); do{ System.out.println("******************************"); if(userNames[0]==null){ System.out.println("Please enter the action you want to perform (you have not logged in to the account yet, please register first)"); }else{ System.out.println("Please enter the action you want to perform (you have registered an administrator and can log in to the system)"); } System.out.println("1. Register an account 2. Log in to the system 3. Forgot your password 4. Log out of the system"); String num1 = sc.next(); switch(num1){ case "1": do{ System.out.println("Please enter the user name you want to register"); userNames[stuNum] = sc.next(); System.out.println("Please enter the password you want to register"); String password1 = sc.next(); System.out.println("Please enter the password you want to register"); String password1 = sc.next(); System.out.println("Please enter the password again"); String password2 = sc.next(); if(password1.equals(password2)){ passwords[stuNum] = password1; System.out.println("Congratulations! Account number: "+userNames[stuNum]+"Created successfully"); flag = true; }else{ System.out.println("Please recreate the password when entered twice inconsistently"); flag = false; } }while(!flag); stuNum++; continue; case "2": flag = false; break; case "3": System.out.println("Please enter the account number to query for password"); String username1 = sc.next(); for(int i=0;i<userNames.length;i++){ if(username1.equals(userNames[i])){ System.out.println("Account"+username1+" is "+passWords[i]); aa = false; } flag = true; } if(aa == true){ System.out.println("No such account information"); aa = true; flag = true; } continue; case "4": System.exit(0); flag = false; default: System.out.println("Please re-enter if input is incorrect"); flag = true; } }while(flag); do{ System.out.println("Please enter login account"); String username = sc.next(); System.out.println("Please enter password"); String password = sc.next(); for(int i=0;i<userNames.length;i++){ if(username.equals(userNames[i])&&password.equals(passWords[i])){ System.out.println("Welcome"+username); ss = false; break; } } if(ss==true){ System.out.println("Account or password is incorrect, please log in again"); ss = true; } }while(ss); do{ System.out.println("******************************************************"); System.out.println("Please enter the operation you want to perform"); System.out.println("************************************************************************************); System.out.println("1. Add student information 2. Delete student information 3. Modify student name 4. View student information 5. Exit the system"); System.out.println("***************************************************************************************************); String num2 = sc.next(); switch(num2){ case "1": System.out.println("Please enter the student name to be added"); stuName[stuNum] = sc.next(); System.out.println("Please enter the student number of the student you want to add"); stuId[stuNum] = sc.next(); stuNum++; System.out.println("Add successfully"); flag = true; break; case "2": System.out.println("Please enter the student number of the student you want to delete"); String stuIdDel = sc.next(); for(int i=0;i<stuName.length;i++){ if(stuIdDel.equals(stuId[i])){ stuId[i] = stuId[stuId.length-1]; stuName[i] = stuName[stuName.length-1]; System.out.println("Delete successfully!"); flag = true; cc = false; } }if(cc == true){ System.out.println("No student number for this student"); flag = true; } break; case "3": System.out.println("Please enter the student number of the student you want to modify"); String stuIdUp = sc.next(); for(int i=0;i<stuId.length;i++){ if(stuIdUp.equals(stuId[i])){ System.out.println("Please enter the new student name"); String stuNameUp = sc.next(); stuName[i] = stuNameUp; System.out.println("Modification was successful!"); flag = true; bb = false; } } if(bb==true){ System.out.println("No student number for this student"); flag = true; }break; case "4": for(int i=0;i<stuName.length;i++){ if(stuName[i]==null){ continue; } System.out.println("The student's name is "+stuName[i]+"/t The student's student number is "+stuId[i]); } flag = true; break; case "5": System.exit(0); default: System.out.println("If the input is incorrect, please re-enter!!!"); flag = true; } } while(flag); } }This code is written by Java juniors. The method is not used very properly and is for entertainment only.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.