JavaRoad
- Java应该算是入门了,前几天写了个 教师信息管理。
- 感觉Java基本应该比较简单,Java有太多的方法,个人感觉非常的nice。
- 每天打开学习
- 最难不过坚持
5.代码区
//老师信息类
public class teacher {
private String m_Name;
private int m_Age;
private int m_Sex; //1----男;0-----女
private long m_Salary;
private String m_Address;
public String getName() {
return m_Name;
}
public void getMsg(String name, int age, int sex, long salary, String address) {
this.m_Name = name;
this.m_Age = age;
this.m_Sex = sex;
this.m_Salary = salary;
this.m_Address = address;
}
public void setName(String name) {
this.m_Name = name;
}
public void setAge(int age) {
this.m_Age = age;
}
public void setSex(int sex) {
this.m_Sex = sex;
}
public void setSalary(long salary) {
this.m_Salary = salary;
}
public void setAddress(String address) {
this.m_Address = address;
}
public void show() {
if (m_Sex == 0) {
System.out.println("老师的姓名:" + m_Name + ",年龄为:" + m_Age + "岁,性别是: 男, 拥有的工资是:" + m_Salary + ", 家庭地址是:" + m_Address);
} else if (m_Sex == 1) {
System.out.println("老师的姓名:" + m_Name + ",年龄为:" + m_Age + "岁,性别是: 女, 拥有的工资是:" + m_Salary + ", 家庭地址是:" + m_Address);
} else {
System.out.println("老师的姓名:" + m_Name + ",年龄为:" + m_Age + "岁,性别是: 人妖, 拥有的工资是:" + m_Salary + ", 家庭地址是:" + m_Address);
}
}
}
基础入门还是比较简单
//java代码
```import java.util.ArrayList;
import java.util.Scanner;
public class test {
public static void main(String[] args) {
ArrayList<teacher> arr = new ArrayList<>();
while (true) {
System.out.println("1--------添加老师的信息");
System.out.println("2--------删除老师的信息");
System.out.println("3--------查询老师的信息");
System.out.println("4--------展示所以老师的信息");
System.out.println("5--------修改老师的信息");
System.out.println("0--------退出");
Scanner sc = new Scanner(System.in);
int choose = sc.nextInt();
if (choose == 0) {
System.out.println("关闭教师信息库");
break;
}
switch (choose) {
case 1:
inputTeacher(arr);
continue;
case 2:
deleteTeacher(arr, "");
continue;
case 3:
showTeacher(arr);
continue;
case 4:
showAll(arr);
continue;
case 5:
changeTeacher(arr, "");
}
}
}
//输入教职工的信息
public static void inputTeacher(ArrayList<teacher> arr) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入老师的姓名:");
String name = sc.nextLine();
System.out.print("请输入老师的年龄:");
int age = sc.nextInt();
System.out.print("请输入老师的性别(0-->男,1-->女):");
int sex = sc.nextInt();
System.out.print("请输入老师的工资:");
long salary = sc.nextLong();
sc.nextLine();//读取\n
System.out.print("请输入老师的地址:");
String address = sc.nextLine();
teacher t = new teacher();
t.getMsg(name, age, sex, salary, address);//给类的成员赋值
arr.add(t);//add到ArrayList
}
//查询已经输入的教职工的信息
public static void showTeacher(ArrayList<teacher> arr) {
if (arr.size() == 0) {
System.out.println("老师信息库里面没人任何信息");
return;
}
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要查询的老师的姓名");
String name = sc.nextLine();
for (int i = 0; i < arr.size(); i++) {
if (arr.get(i).getName().equals(name)) {
System.out.println("找到了你所要查询的老师了,下面是老师的信息");
arr.get(i).show();
System.out.println("你是否要删除老师的信息0--->是,1--->否");
int number = sc.nextInt();
if (number == 0) {
deleteTeacher(arr, name);
return;
} else {
return;
}
}
}
System.out.println("暂时没有老师的信息,是否要添加?0--->是,1--->否");
int number = sc.nextInt();
if (number == 0) {
inputTeacher(arr);
}
}
//删除老师信息
public static void deleteTeacher(ArrayList<teacher> arr, String name1) {
if (arr.size() == 0) {
System.out.println("老师信息库里面没人任何信息");
return;
}
if (name1 == "") {
System.out.println("请输入你要删除老师的名字");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
for (int i = 0; i < arr.size(); i++) {
if (arr.get(i).getName().equals(name)) {
arr.remove(i);
System.out.println("删除了名为" + name + "的老师");
break;
}
}
System.out.println("老师的信息库里面没有你所要查询的老师的信息");
} else {
for (int i = 0; i < arr.size(); i++) {
if (arr.get(i).getName().equals(name1)) {
arr.remove(i);
System.out.println("删除了名为" + name1 + "的老师");
break;
}
}
}
}
//展示所有老师的信息
public static void showAll(ArrayList<teacher> arr) {
if (arr.size() == 0) {
System.out.println("老师信息库里面没人任何信息");
return;
}
for (int i = 0; i < arr.size(); i++) {
arr.get(i).show();
}
}
//
public static void changeTeacher(ArrayList<teacher> arr, String name1) {
if (arr.size() == 0) {
System.out.println("老师信息库里面没人任何信息");
return;
} else {
if (name1 == "") {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要修改老师的名字");
String name = sc.nextLine();
for (int i = 0; i < arr.size(); i++) {
if (arr.get(i).getName().equals(name)) {
System.out.println("根据下面提示选择修改项");
while (true) {
System.out.println("1-----姓名");
System.out.println("2-----年龄");
System.out.println("3-----性别");
System.out.println("4-----工资");
System.out.println("5-----地址");
System.out.println("0-----退出");
System.out.print("请输入: ");
int N = sc.nextInt();
sc.nextLine();
if (N == 0) {
System.out.println("退出了修改系统");
break;
}
switch (N) {
case 1:
System.out.println("请输入新的名字:");
String name2 = sc.nextLine();
arr.get(i).setName(name2);
System.out.println("修改名字成功");
break;
case 2:
System.out.println("请输入新的工资:");
int age = sc.nextInt();
arr.get(i).setAge(age);
System.out.println("修改年龄成功");
break;
case 3:
System.out.println("请输入新的性别:");
int sex = sc.nextInt();
arr.get(i).setSex(sex);
System.out.println("性别修改成功");
break;
case 4:
System.out.println("请输入新的工资:");
long salary = sc.nextLong();
arr.get(i).setSalary(salary);
System.out.println("工资修改成功");
break;
case 5:
System.out.println("请输入新的地址:");
String address = sc.nextLine();
arr.get(i).setAddress(address);
System.out.println("地址修改成功");
break;
}
}
}
}
}
}
}
}
6.都看到这个位置了,如果有什么问题,欢迎留言,记得点个赞哦。
7.各位大佬,祝你们年薪百万不是梦。