Gpa显示和最低等级第一?

时间:2015-03-25 08:59:01

标签: java

你好我有这个问题,我已经磕了很多天了,我会把我试过的整个代码放进去... 这是第一个问题: 作为一名兼职学生,你上学期选修了两门课程。编写一个Java程序,计算并显示该术语的平均成绩(GPA)。您的课程应提示用户输入每门课程的成绩和学分。然后,应首先显示最低等级的此信息,并计算和显示该术语的GPA。如果GPA低于2.0,则应打印警告消息;如果GPA为3.5或更高,则应打印祝贺消息。

首先显示最低成绩的部分真的令我感到困惑

import java.util.*;
public class Gpa
{static Scanner stdIn= new Scanner(System.in);
public static void main (String[]args)
{
  char grade1=' ';
  char grade2=' ';
  double gradevalue1=0;
  double gradevalue2=0;
  double gpa;
  double totalpointscourse1;
  double totalpointscourse2;
  double credithours1,credithours2;
  System.out.print("enter grade for course1 like A,B,C,D,F: ");
  grade1=stdIn.next().charAt(0);
  System.out.print("enter credit hours for course1: ");
  credithours1=stdIn.nextDouble();
  System.out.print("enter grade for course2 like A,B,C,D,F: ");
  grade2=stdIn.next().charAt(0);
  System.out.print("enter credit hours for course2: ");
  credithours2=stdIn.nextDouble();

  if(grade1=='A')
     gradevalue1=4;
  else 
     if(grade1=='B')
        gradevalue1=3;
     else if (grade1=='C')
        gradevalue1=2;
     else if (grade1=='D')
        gradevalue1=1;
     else 
        gradevalue1=0;




  if(grade2=='A')
     gradevalue2=4;
  else
     if (grade2=='B')
        gradevalue2=3;
     else if (grade2=='C')
        gradevalue2=2;
     else if (grade2=='D')
        gradevalue2=1;
     else 
        gradevalue2=0;


  totalpointscourse1=gradevalue1*credithours1;
  totalpointscourse2=gradevalue2*credithours2;
  gpa= (totalpointscourse1+totalpointscourse2)/(credithours1+credithours2);

  if(grade1=='A'||grade1=='B'||grade1=='C'||grade1=='D'||grade1=='F'||grade2=='A'||grade2=='B'||
  grade2=='C'||grade2=='D'||grade2=='F')
     System.out.println(" your gpa is" + gpa);
  else
     System.out.println(" invalid gpa");


  if(gpa>=3.5)
     System.out.println("Congratulations");
  else if (gpa<2.0)
     System.out.println("WARNING");

  if (grade1>grade2) {
     System.out.println(grade2); 
     System.out.println(grade1); 

  } 
  else {
     System.out.println(grade1); 
     System.out.println(grade2); 
  }


}
}

这是我的输出:

//

enter grade for course1 like A,B,C,D,F: B

enter credit hours for course1: 3

enter grade for course2 like A,B,C,D,F: A

enter credit hours for course2: 3

 your gpa is3.5
Congratulations
A
B
//

我正在使用jGrasp btw

1 个答案:

答案 0 :(得分:0)

您完成了这项工作,只需将循环条件从>更改为<即可。但是你得到的方法很简单,比较运算符实际上比较了字符的unicodes,因为字母的unicodes是顺序的,B来自A因此B的unicode更大比A的。