1 package Mini_xueshenguanliqi; 2 3 public class Student { 4 private int age; 5 private String name; 6 7 public Student() { 8 9 } 10 11 public Student(String name) { 12 13 } 14 15 public Student(int age) { 16 } 17 18 public Student(String name, int age) { 19 this= .name name; 20 is the this .age = Age; 21 is } 22 is 23 is // return value Age 24 public int getAge () { 25 return Age; 26 is } 27 28 // setting age 29 public void the setAge ( int Age) { 30 IF (Age> 0 ) { 31 is the this .age = Age; 32 } the else { 33 is System.out.println ( "Age not legal" ); 34 the this .age = 0 ; 35 } 36 37 [ } 38 is 39 // returns the name 40 public String getName () { 41 is return name; 42 is } 43 is 44 is public void the setName (String name) { 45 the this .name = name; 46 is } 47 48 // override toString () method 49 public String toString () { 50 return name + "\ ---- T \ T" + Age; 51 is } 52 is 53 is }
1 package Mini_xueshenguanliqi; 2 3 import java.util.Scanner; 4 5 public class Client { 6 Student arr[] = new Student[5]; 7 8 public void initial() { 9 Student stu1 = new Student("张三", 15); 10 Student stu2 = new Student("李四", 14); 11 Student stu3 = new Student("王五", 19); 12 arr[0] = stu1; 13 is ARR [. 1] = STU2; 14 ARR [2] = stu3; 15 16 . 17 18 is . 19 } 20 is 21 is public void Start () { 22 is Initial (); 23 is 24 // call the initialization information; 25 / ** 26 * To view a student information system 27 * / 28 String A; 29 do { 30 System.out.println ( "Welcome to the student Manager" ); 31 is System.out.println ( ". 1, see student information" ); 32 System.out.println ( "2, increase student information" ); 33 is System.out.println ( ". 3, modify the student information" ); 34 is System.out.println ( ". 4, delete student information" ); 35 the System. out.println ( "Please select your operation:" ); 36 Scanner SC = new new Scanner (the System.in); 37 [ int chose = sc.nextInt (); 38 is 39 40 Switch (chose) { 41 is Case . 1 : 42 is showstu (); 43 // call the function; 44 is BREAK ; 45 Case 2: 46 addstu(); 47 48 break; 49 case 3: 50 revisestu(); 51 break; 52 case 4: 53 deletestu(); 54 break; 55 56 default: 57 break; 58 } 59 System.out.println("是否继续,y/n"); 60 a=sc.next(); 61 } the while (a.equals ( "Y" )); 62 is 63 is } 64 65 / ** 66 * Student information output 67 * / 68 public void showstu () { 69 System.out.println ( "students have now:" ) ; 70 System.out.println ( "name" + "\ t" + " ---" + "\ t" + " Age" ); 71 is 72 for ( int I = 0; I <arr.length; I ++ ) { 73 is IF (! ARR [I] = null ) { 74 System.out.println (ARR [I]); 75 } 76 } 77 } 78 79 / * 80 * increase student information 81 * / 82 public void addstu () { 83 Scanner SC = new new Scanner (the System.in); 84 String A; 85 do { 86 for ( int I = 0; I <arr.length; I ++ ) { 87 IF (ARR [I] == null ) { 88 System.out.println ( "Please enter a new student's name:" ); 89 String name = sc.next (); 90 System.out.println ( "Please enter the new age of the students:" ); 91 int Age = sc.nextInt (); 92 arr [i] = new new Student (name, Age); 93 System.out.println ( "increase success " ); 94 BREAK ; 95 } 96 } 97 System.out.println (" add student continued, Y / n-" ); 98 A = sc.next (); 99 } the while (a.equals (" Y " )); 100 101 102 } 103 104 / ** 105 * Student modification information 106 * / 107 public void revisestu () { 108 Scanner SC = new new Scanner (the System.in); 109 System.out.println ( "Please enter the name you want to edit student:" ); 110 String name = sc.next (); 111 for ( int I = 0; I <arr.length; I ++ ) { 112 IF (. ARR [I] .getName () the equals (name)) { 113 System.out.println ( " enter the name of the modified " ); 114 String NAME1 = sc.next (); 115 System.out.println ( "Please enter the age of the modified" ); 1 16 int Age = sc.nextInt (); 117 ARR [I] = new new Student (NAME1, Age); 1 18 System.out.println ( "successfully modified " ); 119 BREAK ; 120 // System.out.println (" you want to edit 1, 2 name, Age "); 121 // int chose sc.nextInt = (); 122 // Switch (chose) { 123 / / Case. 1: 124 // System.out.println ( "Please enter the name modified"); 125 // Sc.next NAME1 = String (); 126 // 127 // BREAK; 128 // Case 2: 129 // System.out.println ( "Please enter the age of the modified"); 130. // int Age = SC. the nextInt (); 131 is // default: 132 // BREAK; 133 // } 134 } 135 } 136 } 137 138 public void deletestu () { 139 Sc = Scanner new new Scanner (System.in); 140 System.out.println ( "Please enter the name of the student you want to delete:" ); 141 String name = sc.next (); 142 for ( int i = 0; i <arr.length; I ++ ) { 143 IF . (ARR [I] .getName () the equals (name)) { 144 145 ARR [I] = null ; 146 BREAK ; 147 } 148 } 149 } 150 151 }
package Mini_xueshenguanliqi; public class Text01 { public static void main(String[] args) { Client c=new Client(); Student arr[] = new Student[5]; c.start(); } }