之前学习语言的时候,都没有认真的敲,现在有了思路,又有了老师的指导,进步还是蛮快 的,不过一些概念的理解,还是要靠自己理解,包括思路的整理,笔记的书写,而且敲的过程中,就会思考为什么是这样的,这样就会进步很大,比如最近做的如下程序。
import java.util.ArrayList;
import java.util.Scanner;
/**
* @author Peter.Shen
* @date 2019-08-20 14:41
*/
public class StudentManager {
public static void main(String[] args) {
ArrayList<Students> arry = new ArrayList<Students>();
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.println("-----------------------------");
System.out.println("请输入您的选择:");
Scanner input = new Scanner(System.in);
int choice = input.nextInt();
switch (choice) {
case 1:
addStudent(arry);
break;
case 2:
deleteStudent(arry);
break;
case 3:
modifyStudent(arry);
break;
case 4:
checkStudent(arry);
break;
case 5:
System.out.println("5.退出系统");
System.exit(0);
}
}
}
public static void addStudent(ArrayList<Students> arry) {
Scanner input = new Scanner(System.in);
String id="";
if (arry.size() == 0) {
System.out.println("请输入学号:");
id = input.nextLine();
} else if(arry.size() > 0) {
System.out.println("请输入学号:");
id = input.nextLine();
boolean flag = true;
while (flag == true) {
for (int i = 0; i < arry.size(); i++) {
Students s = arry.get(i);
if (s.getId().equals(id)) {
flag = false;
System.out.println("此学号已存在,请重新输入");
System.out.println("请重新输入学号:");
id = input.nextLine();
break;
}
}
if (flag == true) {
break;
}
}
}
System.out.println("请输入姓名:");
String name = input.nextLine();
System.out.println("请输入年龄:");
String age = input.nextLine();
System.out.println("请输入地址:");
String address = input.nextLine();
Students stu = new Students();
stu.setId(id);
stu.setName(name);
stu.setAge(age);
stu.setAddress(address);
arry.add(stu);
System.out.println("添加学生成功");
}
public static void deleteStudent(ArrayList<Students> arry) {
boolean flag = true;
while (flag == true) {
System.out.println("请输入要删除学生的学号");
Scanner input = new Scanner(System.in);
String num = input.nextLine();
for (int i = 0; i < arry.size(); i++) {
Students s = arry.get(i);
if (s.getId().equals(num)) {
arry.remove(i);
flag = false;
System.out.println("删除学生成功");
}
}
if (flag == true) {
System.out.println("此学号不存在,请重新输入");
}
}
}
public static void modifyStudent(ArrayList<Students> arry) {
boolean flag = true;
String sid ="";
Scanner input = new Scanner(System.in);
while (flag == true) {
System.out.println("请输入要修改学生信息的学号:");
sid = input.nextLine();
for (int i = 0; i < arry.size(); i++) {
Students s = arry.get(i);
if (s.getId().equals(sid)) {
flag = false;
}
}
if (flag == true) {
System.out.println("此学号不存在,请重新输入");
}
}
Students stu = new Students();
stu.setId(sid);
System.out.println("请输入新姓名");
String name = input.nextLine();
stu.setName(name);
System.out.println("请输入新年龄");
String age = input.nextLine();
stu.setAge(age);
System.out.println("请输入新地址");
String address = input.nextLine();
stu.setAddress(address);
for (int i = 0; i < arry.size(); i++) {
Students s = arry.get(i);
if (s.getId().equals(sid)) {
arry.set(i, stu);
System.out.println("学生信息修改成功");
break;
}
}
}
public static void checkStudent(ArrayList<Students> arry) {
if (arry.size() == 0) {
System.out.println("信息还未录入,无法查看");
System.out.println("");
System.out.println("");
return;
}
System.out.println("学号" + "\t\t" + "姓名" + "\t\t" + "年龄" + "\t\t" + "地址");
for (int i = 0; i < arry.size(); i++) {
Students s = arry.get(i);
System.out.println(s.getId() + "\t\t" + s.getName() + "\t" + s.getAge() + "\t" + s.getAddress());
}
}
}
当时调了很多次,就觉得很难,怎么都没过,而且很多情况都没有考虑到位,最后在老师的指导下,完成了,觉得很有成就感。开心呢! |
|