我想实现一个程序,以在一个学期的班级中返回学生的及格状态。假设他在3学分的班级中有A,在4学分的班级中有B,在3学分的班级中有F。如何计算学生通过的学分数量?
条件是只要他的GPA高于1.33(或D-),他就会通过。换句话说,如果他获得A,那么他不会通过。 我的代码现在似乎无法正常工作。
if (gradeValue >= 1.33)
pass = totalunits;
答案 0 :(得分:0)
这是我的另一个……还没有。
if (grade[i].equals("F"))
pass = totalunits - Units[i];
gpa = totalgradepoints / totalunits;
答案 1 :(得分:0)
这是我的完整答案。剩下的唯一一件事是我不知道如何保留小数点后三位...我会在提出一些建议时进行更新。
import java.util.*;
import java.text.*;
import java.lang.Math;
public class Project1_Trial3 {
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
//Input the term
System.out.println("Please enter the term of your grade calculation (for example, Fall 2015): ");
String term = scanner.nextLine();
//Input the number of courses that the student is enrolled in
System.out.println("Please enter the number of courses that you are enrolled in "+term+": ");
int numberofcourses = scanner.nextInt();
//Declaration
String ClassName[] = new String[numberofcourses];
String Description[] = new String[numberofcourses];
String grade[] = new String[numberofcourses];
float Units[] = new float[numberofcourses];
double gradeValue = 0;
float finalgrade[] = new float[numberofcourses];
float totalunits = 0;
float fail = 0;
float pass = 0;
float totalgradepoints = 0;
float gpa = 0;
String standing = null;
//Arrays for class number, description, units, grade, grade point
//Here, input class number, description, units, and grade
for(int i = 0; i < numberofcourses; i++)
{
scanner.nextLine();
System.out.println("Please enter your #"+(i+1)+" class name: ");
ClassName[i] = scanner.nextLine();
System.out.println("Please enter your #"+(i+1)+" class description: ");
Description[i] = scanner.nextLine();
System.out.println("Please enter your #"+(i+1)+" class units: ");
Units [i] = scanner.nextInt();
scanner.nextLine();
System.out.println("Please enter your #"+(i+1)+" class grade: ");
grade[i] = scanner.nextLine();
if (grade[i].equals ("A"))
gradeValue= 4.00;
else if (grade[i].equals("A-"))
gradeValue= 3.67;
else if (grade[i].equals("B+"))
gradeValue = 3.33;
else if (grade[i].equals("B"))
gradeValue = 3.00;
else if (grade[i].equals ("B-"))
gradeValue = 2.67;
else if (grade[i].equals("C+"))
gradeValue = 2.33;
else if (grade[i].equals("C"))
gradeValue = 2.00;
else if (grade[i].equals ("D+"))
gradeValue = 1.33;
else if (grade[i].equals ("D"))
gradeValue = 1.00;
else if (grade[i].equals ("F"))
gradeValue = 0;
else
System.out.println ("Invalid Grade");
finalgrade[i] = (float) (gradeValue * Units[i]);
//Get the total number of units
totalunits += Units[i];
//Get the total grade points
totalgradepoints += finalgrade[i];
if (grade[i].equals("F"))
fail = Units[i];
pass = totalunits - fail;
gpa = totalgradepoints / totalunits;
}
//Get status
if (gpa > 2.0)
standing = "Good standing";
else if (1.0 < gpa | gpa < 1.999)
standing = "On probation";
else
standing = "Dismissed";
//Round to 3 decimals and get final results
//Finally, print out the report
//Print out the heading
System.out.println("Class Grades - "+term+" Term");
System.out.println("Office Grades");
//Print out the table
System.out.println(String.format("%20s %7s %25s %7s %10s %7s %7s %10s %10s", "Class", "|", "Description", "|", "Units", "|", "Grade", "|", "Gradepoint"));
System.out.println(String.format("%s", "-------------------------------------------------------------------------------------------------------------------------------------"));
for(int i = 0; i < numberofcourses; i++)
{
DecimalFormat format = new DecimalFormat("#.#####");
format.format(totalunits);
format.format(fail);
format.format(totalgradepoints);
format.format(gpa);
System.out.println(String.format("%20s %7s %-25s %7s %10s %7s %-7s %10s %-10s", ClassName[i], "|", Description[i], "|", Units [i], "|", grade[i], "|", finalgrade[i]));
}
System.out.println("Term Statistics - "+term+" Term");
System.out.println(String.format("%s", "-----------------------------------------------------------------------------------------------"));
System.out.println(String.format("%40s %7s %25s", "", "|", "From Enrollment"));
System.out.println(String.format("%40s %7s %25s", "", "|", ""));
System.out.println(String.format("%40s %7s %25s", "Units Towards GPA:", "|", ""));
System.out.println(String.format("%40s %7s %25s", "Taken", "|", totalunits));
System.out.println(String.format("%40s %7s %25s", "Passed", "|", pass));
System.out.println(String.format("%s", "-----------------------------------------------------------------------------------------------"));
System.out.println(String.format("%40s %7s %25s", "GPA Calculation", "|", ""));
System.out.println(String.format("%40s %7s %25s", "Total Grade Points", "|", totalgradepoints));
System.out.println(String.format("%40s %7s %25s", "/ Units Taken Toward GPA", "|", totalunits));
System.out.println(String.format("%s", "-----------------------------------------------------------------------------------------------"));
System.out.println(String.format("%40s %7s %25s", "= GPA", "|", gpa));
System.out.println(String.format("%40s %7s %25s", "Status", "|", standing));
}
}
答案 2 :(得分:-1)
虽然我猜想您只是将这个答案撕开并上交,但我还是写了它。权重计算取自gpacalculator.net。如果这是生产代码,那么会有更好的注释,封装等。
注意:其中某些条目需要滚动...
enum Grade
{
A ("A", 4.00),
A_MINUS ("A-", 3.67),
B_PLUS ("B+", 3.33),
B ("B", 3.00),
B_MINUS ("B-", 2.67),
C_PLUS ("C+", 2.33),
C ("C", 2.00),
C_MINUS ("C-", 1.67),
D_PLUS ("D+", 1.33),
D ("D", 1.00),
D_MINUS ("D-", 0.67),
F ("F", 0.00);
private final String gradeString;
private final double gradeGPA;
Grade(String gradeString, double gradeGPA)
{
this.gradeString = gradeString;
this.gradeGPA = gradeGPA;
}
public String getGradeString()
{
return gradeString;
}
public double getGradeGPA()
{
return gradeGPA;
}
public static String getGradeLetter(double gpa)
{
if (gpa == A.gradeGPA) return A.gradeString;
if (gpa >= A_MINUS.gradeGPA) return A_MINUS.gradeString;
if (gpa >= B_PLUS.gradeGPA) return B_PLUS.gradeString;
if (gpa >= B.gradeGPA) return B.gradeString;
if (gpa >= B_MINUS.gradeGPA) return B_MINUS.gradeString;
if (gpa >= C_PLUS.gradeGPA) return C_PLUS.gradeString;
if (gpa >= C.gradeGPA) return C.gradeString;
if (gpa >= C_MINUS.gradeGPA) return C_MINUS.gradeString;
if (gpa >= D_PLUS.gradeGPA) return D_PLUS.gradeString;
if (gpa >= D.gradeGPA) return D.gradeString;
if (gpa >= D_MINUS.gradeGPA) return D_MINUS.gradeString;
return F.gradeString;
}
}
class Course
{
String courseName;
Grade grade;
int credits;
public Course(String courseName, Grade grade, int credits)
{
this.courseName = courseName;
this.grade = grade;
this.credits = credits;
}
}
public class GPACalculator
{
public static double calculateGPA(List<Course> courses)
{
double totalGPA = 0.0;
int totalCredits = 0;
for (Course course : courses)
{
totalGPA += course.grade.getGradeGPA() * course.credits;
totalCredits += course.credits;
}
return totalGPA / totalCredits;
}
public static void main(String[] args)
{
DecimalFormat prettyFormat = new DecimalFormat("0.00");
List<Course> courses = new ArrayList<>();
Course calculusOne = new Course("Calculus I", Grade.B_MINUS, 4);
Course dataStructures = new Course("Data Structures", Grade.B, 3);
Course pointlessEnglish = new Course("English", Grade.D_PLUS, 3); // class was boring and I didn't do any homework.
Course norseMythology = new Course("Norse Mythology", Grade.A, 3);
courses.add(calculusOne);
courses.add(dataStructures);
courses.add(pointlessEnglish);
courses.add(norseMythology);
double gpa = calculateGPA(courses);
if (gpa >= Grade.D_PLUS.getGradeGPA())
System.out.println("You passed.");
else
System.out.println("You failed.");
System.out.println("Your GPA was: " + prettyFormat.format(gpa));
// TODO: add something funny about working too hard or playing too hard...
}
}