展开全部
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
//名字;—
//课程名称#1;—-
//总共学分:
//成绩:使用ABCDF计算格式;A=4分, B=3.。。
//有没有e69da5e887aa3231313335323631343130323136353331333239303865其他课程#2:Yes/no 名称—
//
//显示:
//
//名字:XXX
//课程 学分 成绩
//计算机课 4 B+
//数学 5 B
//平均点数GPA 3.0
class Course {
private String course;
private String unit;
private String grade;
private String score;
public String getCourse() {
return this.course;
}
public void setCourse(String course) {
this.course = course;
}
public String getUnit() {
return this.unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
public String getGrade() {
return this.grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public String getScore() {
return this.score;
}
public void setScore(String score) {
this.score = score;
}
}
class GPAInfo {
private String name;
private List courseInfo;
private String gpa;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public List getCourseInfo() {
return this.courseInfo;
}
public void setCourseInfo(List courseInfo) {
this.courseInfo = courseInfo;
}
public String getGpa() {
return this.gpa;
}
public void setGpa(String gpa) {
this.gpa = gpa;
}
}
public class GPA {
public static void main(String[] args) {
String hasNextStudent = “y”;
String hasNextCourse = “y”;
List gpaInfolist = new ArrayList();
while((hasNextStudent != null) && “y”.equals(hasNextStudent.toLowerCase())) {
GPAInfo gpaInfo = new GPAInfo();
String name = JOptionPane.showInputDialog(“enter a name”);
gpaInfo.setName(name);
List courselist = new ArrayList();
while ((hasNextCourse != null) && “y”.equals(hasNextCourse.toLowerCase())) {
Course course = new Course();
String courseName = JOptionPane.showInputDialog(” What class do you have?”);
course.setCourse(courseName);
String unit = JOptionPane.showInputDialog(” Enter the units you get”);
course.setUnit(unit);
String grade = JOptionPane.showInputDialog(” Grade you get?”);
course.setGrade(grade);
course.setScore(String.valueOf(getScore(grade.toCharArray()[0])));
courselist.add(course);
hasNextCourse = JOptionPane.showInputDialog(” other class? Yes or no”);
}// while has next course
hasNextCourse = “y”;
gpaInfo.setCourseInfo(courselist);
gpaInfo.setGpa(getGpa(courselist));
gpaInfolist.add(gpaInfo);
hasNextStudent = JOptionPane.showInputDialog(” other student? Yes or no”);
}// while has next student
String output = “”;
for (GPAInfo gpaInfo : gpaInfolist) {
output += “名字:” + gpaInfo.getName() + “\n”;
output += “课程 学分 成绩” + “\n”;
List courselist = gpaInfo.getCourseInfo();
for (Course course : courselist) {
output += course.getCourse() + ” ” + course.getUnit() + ” ” + course.getScore() + “\n”;
}
output += “平均点数GPA ” + gpaInfo.getGpa() + “\n\n”;
}
JOptionPane.showMessageDialog(null, output);
System.exit(-1);
}
private static String getGpa(List courselist) {
double avg = 0;
int totalScore = 0;
int totalUnit = 0;
for (Course course : courselist) {
totalUnit += Integer.parseInt(course.getScore());
totalScore += Integer.parseInt(course.getUnit()) * Integer.parseInt(course.getScore());
}
if (totalUnit != 0) {
avg = totalScore / totalUnit;
}
if (avg > 4) {
avg = 4;
}
return String.valueOf(avg);
}
private static int getScore(char score) {
int point;
score = Character.toUpperCase(score);
switch (score) {
case ‘A’:
point = 4;
break;
case ‘B’:
point = 3;
break;
case ‘C’:
point = 2;
break;
case ‘D’:
point = 1;
break;
default:
point = 0;
break;
}
return point;
}
}
已赞过
已踩过<
你对这个回答的评价是?
评论
收起