java计算课程学分绩点_计算GPA和学分的java程序

这是一个Java程序,用于输入学生课程名称、学分和成绩(以A-F字母等级),然后计算并显示学生的平均绩点GPA。程序通过对话框交互获取数据,并能处理多个学生和课程的情况。
摘要由CSDN通过智能技术生成

展开全部

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;

}

}

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值