需求
一、定義學(xué)生類
學(xué)生類可以包含姓名、成績(jī)、學(xué)號(hào)、年齡等等,這里只包含了前兩項(xiàng)學(xué)生類屬性。
public class Student {
//封裝學(xué)生類型:
private String name; //姓名
private int score; //成績(jī)
public Student() {
}
public Student(String name, int score) {
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
//重寫toString方法:
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", score=" + score +
'}';
}
}
二、定義學(xué)生成績(jī)信息管理類
在該類中定義了簡(jiǎn)單的增、刪、查、改的方法。
其中,遍歷集合元素使用到了for...each循環(huán)以及使用迭代器這兩種常見的方式,
添加學(xué)生信息create方法使用了try..catch語句處理異常。
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class StudentScoreManagement {
public static void main(String[] args) {
System.out.println("歡迎登錄學(xué)生成績(jī)信息管理系統(tǒng)!");
while(true) {
System.out.println();
System.out.println("=====================");
System.out.println("請(qǐng)選擇操作:");
System.out.println("①查詢信息\n②添加信息\n③刪除信息\n④修改信息\n⑤退出系統(tǒng)");
System.out.println("=====================");
Scanner stu = new Scanner(System.in);
int n = stu.nextInt();//接收控制臺(tái)輸入
if (n == 1) {
//查詢
StudentScoreManagement.read();
} else if (n == 2) {
//添加
while (true){
//使用 try...catch 捕獲異常:
try {
StudentScoreManagement.create();
break;
} catch (Exception e) {
// throw new RuntimeException(e.toString()); //拋出異常,程序結(jié)束
System.out.println("輸入了不合理的成績(jī),請(qǐng)重新輸入!"); //程序繼續(xù)執(zhí)行
}
}
} else if (n == 3) {
//刪除
StudentScoreManagement.delete();
} else if (n == 4) {
//修改
StudentScoreManagement.update();
} else if (n == 5) {
//退出
System.out.println("已退出學(xué)生成績(jī)信息管理系統(tǒng)!");
break;
} else {
System.out.println("暫無該項(xiàng)指令,請(qǐng)重新輸入!");
}
}
}
//使用ArrayList集合存放學(xué)生信息數(shù)據(jù)
static List<Student> studentList = new ArrayList<>();
/**增*/
public static void create() throws Exception{ //聲明異常
/*輸入成績(jī)*/
String goon = "Y";
while ("Y".equals(goon)) {
Scanner sc = new Scanner(System.in);
System.out.println("請(qǐng)輸入學(xué)生姓名:");
String name = sc.next();
System.out.println("請(qǐng)輸入學(xué)生成績(jī):");
int score = sc.nextInt();
//拋出異常:
if (score<0 || score>100) {
throw new Exception("輸入的成績(jī)不合理!");
}
Student stu = new Student(name,score);
studentList.add(stu);
System.out.println("是否繼續(xù)錄入?Y/N");
goon = sc.next();
}
}
/**刪*/
public static void delete(){
Scanner sc = new Scanner(System.in);
System.out.println("請(qǐng)輸入想要?jiǎng)h除信息的學(xué)生姓名:");
String dname = sc.next();
//使用迭代器遍歷集合元素
Iterator<Student> it = studentList.iterator();
while (it.hasNext()){
Student st=it.next();
if(st.getName().equals(dname)){
it.remove();
System.out.println("刪除成功!");
}else if (st.getName().equals(dname) == true) {
System.out.println("暫不存在該學(xué)生信息,請(qǐng)重新輸入!");
}
}
}
/**查*/
public static void read(){
if (studentList.isEmpty() == true){
System.out.println("暫無數(shù)據(jù)!");
}
else {
System.out.println("已添加的學(xué)生信息:");
//使用for...each循環(huán)語句遍歷集合元素
for (Student stu : studentList
) {
System.out.println(stu);
}
}
}
/**改*/
public static void update(){
Scanner sc = new Scanner(System.in);
System.out.println("請(qǐng)輸入想要修改的學(xué)生信息(輸入 姓名/成績(jī)): 1、姓名 2、成績(jī) ");
String data = sc.next(); //輸入想要修改的信息
if (data.equals("姓名")) {
System.out.println("請(qǐng)輸入將要修改姓名信息的同學(xué):");
Scanner scanner = new Scanner(System.in);
String name = scanner.next();
//使用迭代器遍歷集合元素
Iterator<Student> it = studentList.iterator();
while (it.hasNext()){
Student st = it.next();
if(st.getName().equals(name)){
System.out.println("請(qǐng)輸入修改后的姓名:");
String newname = scanner.next();
st.setName(newname);
System.out.println("修改成功!\n已修改的學(xué)生信息:");
System.out.println(st);
}else if (st.getName().equals(name) == true) {
System.out.println("暫不存在該學(xué)生信息,請(qǐng)重新輸入!");
}
}
}
else if (data.equals("成績(jī)")){
System.out.println("請(qǐng)輸入將要修改成績(jī)信息的同學(xué)姓名:");
Scanner scanner = new Scanner(System.in);
String name = scanner.next();
//使用迭代器遍歷集合元素
Iterator<Student> it = studentList.iterator();
while (it.hasNext()){
Student st = it.next();
if(st.getName().equals(name)){
System.out.println("請(qǐng)輸入修改后的成績(jī):");
int newscore = sc.nextInt(); //修改后的成績(jī)
st.setScore(newscore);
System.out.println("修改成功!\n已修改的學(xué)生信息:");
System.out.println(st);
}else if (st.getName().equals(name) == true) {
System.out.println("暫不存在該學(xué)生信息,請(qǐng)重新輸入!");
}
}
}
else
System.out.println("您輸入的操作信息有誤!請(qǐng)重新輸入");
}
}
三、運(yùn)行示例
1.添加學(xué)生信息:
歡迎登錄學(xué)生成績(jī)信息管理系統(tǒng)!
=====================
請(qǐng)選擇操作:
①查詢信息
②添加信息
③刪除信息
④修改信息
⑤退出系統(tǒng)
=====================
1
暫無數(shù)據(jù)!
=====================
請(qǐng)選擇操作:
①查詢信息
②添加信息
③刪除信息
④修改信息
⑤退出系統(tǒng)
=====================
2
請(qǐng)輸入學(xué)生姓名:
張三
請(qǐng)輸入學(xué)生成績(jī):
85
是否繼續(xù)錄入?Y/N
Y
請(qǐng)輸入學(xué)生姓名:
李四
請(qǐng)輸入學(xué)生成績(jī):
90
是否繼續(xù)錄入?Y/N
Y
請(qǐng)輸入學(xué)生姓名:
王五
請(qǐng)輸入學(xué)生成績(jī):
70
是否繼續(xù)錄入?Y/N
N
2.查詢信息:
=====================
請(qǐng)選擇操作:
①查詢信息
②添加信息
③刪除信息
④修改信息
⑤退出系統(tǒng)
=====================
1
已添加的學(xué)生信息:
Student{name='張三', score=85}
Student{name='李四', score=90}
Student{name='王五', score=70}
3.刪除信息
=====================
請(qǐng)選擇操作:
①查詢信息
②添加信息
③刪除信息
④修改信息
⑤退出系統(tǒng)
=====================
3
請(qǐng)輸入想要?jiǎng)h除信息的學(xué)生姓名:
王五
刪除成功!
=====================
請(qǐng)選擇操作:
①查詢信息
②添加信息
③刪除信息
④修改信息
⑤退出系統(tǒng)
=====================
1
已添加的學(xué)生信息:
Student{name='張三', score=85}
Student{name='李四', score=90}
3.修改信息:文章來源:http://www.zghlxwxcb.cn/news/detail-507550.html
4
請(qǐng)輸入想要修改的學(xué)生信息(輸入 姓名/成績(jī)): 1、姓名 2、成績(jī)
姓名
請(qǐng)輸入將要修改姓名信息的同學(xué):
張三
請(qǐng)輸入修改后的姓名:
小明
修改成功!
已修改的學(xué)生信息:
Student{name='小明', score=85}
=====================
請(qǐng)選擇操作:
①查詢信息
②添加信息
③刪除信息
④修改信息
⑤退出系統(tǒng)
=====================
4
請(qǐng)輸入想要修改的學(xué)生信息(輸入 姓名/成績(jī)): 1、姓名 2、成績(jī)
成績(jī)
請(qǐng)輸入將要修改成績(jī)信息的同學(xué)姓名:
小明
請(qǐng)輸入修改后的成績(jī):
100
修改成功!
已修改的學(xué)生信息:
Student{name='小明', score=100}
4.退出系統(tǒng):文章來源地址http://www.zghlxwxcb.cn/news/detail-507550.html
=====================
請(qǐng)選擇操作:
①查詢信息
②添加信息
③刪除信息
④修改信息
⑤退出系統(tǒng)
=====================
1
已添加的學(xué)生信息:
Student{name='小明', score=100}
Student{name='李四', score=90}
=====================
請(qǐng)選擇操作:
①查詢信息
②添加信息
③刪除信息
④修改信息
⑤退出系統(tǒng)
=====================
5
已退出學(xué)生成績(jī)信息管理系統(tǒng)!
到了這里,關(guān)于Java基礎(chǔ)——學(xué)生成績(jī)信息管理系統(tǒng)(簡(jiǎn)單實(shí)現(xiàn))的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!