📄 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 + -