十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
//根据等级取得分数
创新互联客户idc服务中心,提供多线BGP机房、成都服务器、成都主机托管、成都双线服务器等业务的一站式服务。通过各地的服务中心,我们向成都用户提供优质廉价的产品以及开放、透明、稳定、高性价比的服务,资深网络工程师在机房提供7*24小时标准级技术保障。
public static double getScore(String rank){
//分数表
int[] scores = new int[]{4,3,2,1,0};
//if else的高效写法
return "A".equalsIgnoreCase(rank)?scores[0]:
"B".equalsIgnoreCase(rank)?scores[1]:
"C".equalsIgnoreCase(rank)?scores[2]:
"D".equalsIgnoreCase(rank)?scores[3]:
"E".equalsIgnoreCase(rank)?scores[4]:
-1 ;
}
//计算平均绩点
public static double getAvg(double[] scores){
if(scores.length != 5){
return -1;
}
int sum = 0;
for (int i = 0; i scores.length; i++) {
sum += scores[i];
}
return sum / 5;
}
public static void main(String[] args) {
System.out.println("请输入五个等级");
String[] ranks = new Scanner(System.in).nextLine().split(" ");
double[] scores = new double[5];
for (int i = 0; i ranks.length; i++) {
scores[i] = getScore(ranks[i]);
}
System.out.println("平均绩点为:" + (getAvg(scores)0?"Unknown":getAvg(scores)));
}
我把方法都封装了一下
数据库里面开一个字段,外面定义一个String变量,当你输入文字(科目)的适合,赋值给这个变量,之后用SQL语句插入到数据库的字段就可以了。
public static void main(String[] args) {
double scores[] = new double[5];
double total = 0;
double avg = 0;
double max = 0;
double min = 0;
int count=0;
String inputStr=null;
System.out.println("请输入5名学生的成绩:");
Scanner input = new Scanner(System.in);
while(count5){
try{
if(count 5){
System.out.println("请输入第"+(count+1)+"个分数:");
}
inputStr=input.nextLine();
scores[count++]=Double.valueOf(inputStr.trim());
}catch(Exception e){
if(inputStr!=null "exit".equals(inputStr.trim())){
System.out.println("您已成功结束程序");
System.exit(0);
}
System.out.println("若想结束请输入:exit");
System.out.print("您输入的分数不是数值类型,");
count--;
}
}
input.close();
Arrays.sort(scores);
min=scores[0];
max=scores[scores.length-1];
for(double score :scores){
total += score;
}
avg=total/scores.length;
System.out.println("总成绩是" + total);
System.out.println("最高分是" + max);
System.out.println("最低分是" + min);
System.out.println("平均分是" + avg);
}
//-------------------------------------------------------------------------
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while(true){
Double[] scores = null;
double total = 0;
double avg = 0;
double max = 0;
double min = 0;
int count=1;
ListDouble inputScores=new ArrayListDouble();
String inputStr=null;
System.out.println("请输入要统计学生的成绩(理论上可以输入无限个,前提是你有那么大的内存):");
while(true){
try{
System.out.println("请输入第"+count+++"个分数,或输入ok进行计算,离开请输入exit");
inputStr=input.nextLine();
inputScores.add((double)Double.valueOf(inputStr.trim()));
}catch(Exception e){
if(inputStr!=null "exit".equals(inputStr.trim().toLowerCase())){
System.out.println("您已成功结束程序");
input.close();
System.exit(0);
}
if(inputStr!=null "ok".equals(inputStr.trim().toLowerCase())){
break;
}
System.out.println("您输入的分数不是数值类型,");
System.out.println("若想结束请输入exit ,若想计算结果请输入ok");
count--;
}
}
if(inputScores.size()==0){
System.out.println("您没有输入学生成绩,无数据可统计,程序结束。");
return ;
}
scores=inputScores.toArray(new Double[inputScores.size()]);
Arrays.sort(scores);
min=scores[0];
max=scores[scores.length-1];
for(double score :scores){
total += score;
}
avg=total/scores.length;
System.out.println("总成绩是" + total);
System.out.println("最高分是" + max);
System.out.println("最低分是" + min);
System.out.println("平均分是" + avg);
}
}
提供一些思路吧,编写一个Student类,其中有各种属性,比如名字:String name;
选修课程:String[] xuanxius;
平均成绩:double avg;
各科成绩:int[] chengjis;
平均成绩的get方法是:for循环各科成绩,进行相加,得到的总成绩再除以chengjis.length;得到的double值就是平均成绩
然后根据平均成绩进行判断,属于哪个段,就把对应的abcd输出就行了
输出就简单了吧,自己输出就行,要是有什么不明白可以追问
姓名和成绩我不知道你想怎样输入,只给出算GPA的算法,输入输出你自己来写吧,如果输入输出你不学的话,我也帮不了你。
//marks是一个学生成绩的数组,hours是该学生选课的学分的数组
private double getGPA(String[] marks, int[] hours)
{
double sumHour = 0;//总学分
double sumHourPoint = 0;//总学分绩点
for (int i = 0; i hours.length; i++)
{
sumHour += hours[i];
sumHourPoint += pointOf(marks[i]) * hours[i];
}
return sumHourPoint / sumHour;//GPA = 总学分绩点/总学分
}
private double pointOf(mark){//计算单科绩点
double point = 0;
switch(mark.charAt(0))
{
case 'A': point = 4;
case 'B': point = 3;
case 'C': point = 2;
case 'D': point = 1;
default: point = 0;
}
if(mark.length() 1)
{
switch(mark.charAt(1))
{
case '+': point += 0.5; break;
default: break;
}
}
return point;
}
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
//名字;---
//课程名称#1;----
//总共学分:
//成绩:使用ABCDF计算格式;A=4分, B=3.。。
//有没有其他课程#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 ListCourse courseInfo;
private String gpa;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public ListCourse getCourseInfo() {
return this.courseInfo;
}
public void setCourseInfo(ListCourse 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";
ListGPAInfo gpaInfolist = new ArrayListGPAInfo();
while((hasNextStudent != null) "y".equals(hasNextStudent.toLowerCase())) {
GPAInfo gpaInfo = new GPAInfo();
String name = JOptionPane.showInputDialog("enter a name");
gpaInfo.setName(name);
ListCourse courselist = new ArrayListCourse();
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";
ListCourse 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(ListCourse 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;
}
}