?? testselectcourse.java
字號:
package shixyan2;
import javax.swing.JOptionPane;
public class TestSelectCourse {
private int num, stuid;
public boolean isExistCourseName(String couName, Course[] course) {
int flag = 0;
for (int j = 0; j < course.length; j++) {
if (couName.equals(course[j].getCourseName())) {
flag = 1;
num = j;
break;
}
}
if (flag == 1) {
return true;
} else
return false;
}
public boolean isExistId(Student[] stu, int id) {
if (id > 1000 && id <= stu.length + 1000) {
for (int j = 0; j < stu.length; j++) {
if (id == stu[j].getStudentId()) {
stuid = j;
}
}
return true;
} else
return false;
}
public boolean isLegalScore(double score) {
if (score >= 0.0 && score <= 100.0) {
return true;
} else
return false;
}
public void find(int id, SelectCourse[] sc, Student[] stu) {
System.out.println("根據學號搜索的結果如下:");
System.out.println("學號:" + id + " 姓名:"
+ stu[id - 1001].getStudentName() + " 選課門數:"
+ stu[id - 1001].getCourseNum());
for (int i = 0; i < sc.length; i++) {
while (id == sc[i].getStudentId()) {
System.out.println("課程名稱:" + sc[i].getCourseName() + " 該課程的分數:"
+ sc[i].getScore());
break;
}
}
// System.out.println("根據學號搜索的結果如下:");
// System.out.println("學號:" + id + " 姓名:"
// + stu[id - 1001].getStudentName() + " 選課門數:"
// + stu[id - 1001].getCourseNum());
// for (int i = 0; course[i] != null; i++) {
// System.out.println("課程名稱:" + course[i] + " 該課程的分數:" + score[i]);
// }
}
public static void main(String[] args) {
TestSelectCourse tsc = new TestSelectCourse();
Student[] stu = new Student[] { new Student("Alice"),
new Student("Bob"), new Student("Cindy"), new Student("tom") };
Course[] course = new Course[] { new Course("java", 3),
new Course("c", 2), new Course("c++", 2) };
SelectCourse[] sc = new SelectCourse[10];
String n = JOptionPane.showInputDialog("您要輸入幾條選課信息(0到10):");
// 對每條選課信息處理
for (int i = 0; i < Integer.parseInt(n); i++) {
int j;
String stuId = JOptionPane
.showInputDialog("請輸入學號(1001~100*,*為學生人數上限[4]):");
String couName = JOptionPane
.showInputDialog("請輸入課程名(1 java,2 c,3 c++):");
String score = JOptionPane.showInputDialog("請輸入分數:");
int id = Integer.parseInt(stuId);
double s = Double.parseDouble(score);
if (!tsc.isExistId(stu, id)) {
JOptionPane.showMessageDialog(null, "對不起,此學號不存在,請重新錄入此條成績!");
i--;
continue;
} else if (!tsc.isExistCourseName(couName, course)) {
JOptionPane.showMessageDialog(null, "對不起,沒有您輸入的課程名,請重新錄入此條成績!");
i--;
continue;
} else if (!tsc.isLegalScore(s)) {
JOptionPane.showMessageDialog(null, "對不起,您輸入的成績不合法,請重新錄入此條成績!");
i--;
continue;
} else
// 滿足條件,錄入成績
sc[i] = new SelectCourse(id, couName, s);
// 判斷是否重復選課!
if (i != 0) {
for (int k = 0; k < i; k++) {
if (sc[i].getStudentId() == sc[k].getStudentId()
&& sc[i].getCourseName().equals(
sc[k].getCourseName())) {
JOptionPane.showMessageDialog(null,
"該生已經選此課程,請重新錄入此條成績!");
i--;
continue;
} else
break;
}
}
// addCourse
course[tsc.num].addCourse(s);
int c = course[tsc.num].getCreditHour();
// 學生課程數+1 重算平均分
stu[tsc.stuid].setCourseNum();
stu[tsc.stuid].setAverageScore(s);
if (s > 60) { // 如果分數及格,將本門課的學分加入總學分
stu[tsc.stuid].setCreditHour(c);
}
}
System.out.println("平均分大于60的學生信息如下:");
for (int i = 0; i < stu.length; i++) {
if (stu[i].getAverageScore() > 60)
stu[i].print();
}
System.out.println("課程信息如下:");
for (int i = 0; i < course.length; i++) {
course[i].print();
}
System.out.println("選課信息如下:");
for (int i = 0; sc[i] != null; i++) {
sc[i].print();
}
String yn = JOptionPane.showInputDialog(null,
"成績錄入完畢,是否要查找學生選課信息?(y/n按確定退出)");
if (yn.equals("y") || yn.equals("Y")) {
String sId = JOptionPane
.showInputDialog("請輸入查找學生的ID號(1001~100*,*為學生人數上限[4]):");
tsc.find(Integer.parseInt(sId), sc, stu);
} else
System.exit(0);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -