📄 testselectcourse.java
字号:
package mystudent;
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;//存在标记为1
num = j;//并记录它的位置
break;
}
}
if (flag == 1) {//标记存在则返回true否则返回false
return true;
} else
return false;
}
public boolean isExistId(Student[] stu, int id) {//判断学生号是否存在
if (id > 00 && id <= stu.length + 00) {
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 - 01].getStudentName() + " 选课门数:"
+ stu[id - 01].getCourseNum());
for (int i = 0; i < sc.length; i++) {
while (id == sc[i].getStudentId()) {//当录入成绩信息里面包含输入的ID,输出信息
System.out.println("课程名称:" + sc[i].getCourseName() + " 该课程的分数:"
+ sc[i].getScore());
break;
}
}
}
public static void main(String[] args) {
TestSelectCourse tsc = new TestSelectCourse();//创建一个新的对象
Student[] stu = new Student[] { new Student("Alice"),//创建一个Student类型的数组
new Student("Bob"), new Student("Cindy"), new Student("tom") };
Course[] course = new Course[] { new Course("java", 3),//Course类型的数组,记录课程的名称和学分
new Course("c", 2), new Course("c++", 2) };
SelectCourse[] sc = new SelectCourse[10];//创建SelectCourse类型的数组,包含10条信息
String n = JOptionPane.showInputDialog("您要输入几条选课信息(0到10):");
// 对每条选课信息处理
for (int i = 0; i < Integer.parseInt(n); i++) {//根据输入的数字进行处理,将字符串转换为整型数值
int j;
String stuId = JOptionPane
.showInputDialog("请输入学号(01~0*,*为学生人数上限[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);//将字符串转换为Double型数值
if (!tsc.isExistId(stu, id)) {//当返回false时
JOptionPane.showMessageDialog(null, "对不起,此学号不存在,请重新录入此条成绩!");
i--;
continue;
} else if (!tsc.isExistCourseName(couName, course)) {//当返回false时
JOptionPane.showMessageDialog(null, "对不起,没有您输入的课程名,请重新录入此条成绩!");
i--;
continue;
} else if (!tsc.isLegalScore(s)) {//当返回false时
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) { // 如果分数及格,将本门课的学分加入总学分,调用student类
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 + -