【问题标题】:Java program to count number of credits that students passed?Java程序来计算学生通过的学分?
【发布时间】:2019-06-22 02:02:26
【问题描述】:

我想实施一个程序,以在一个学期的课程中返回学生的通过状态。比如说,他在 3 学分课程中获得 A,在 4 学分课程中获得 B,在 3 学分课程中获得 F。如何计算学生通过的学分?

条件是只要他的GPA在1.33(或D-)以上,他就通过了。换句话说,如果他得了 A,那么他就不会通过。 我的代码现在似乎不起作用。

    if (gradeValue >= 1.33)
                pass = totalunits;

【问题讨论】:

  • 你可以把String gpa = gpa[i]放在最后......告诉我这是否有效
  • 你想多了。将totalgradepointstotalunits 作为数组是没有意义的,只需使用单个值并在每次迭代中建立一个总和。那么计算 GPA 就很简单了。

标签: java sum iteration


【解决方案1】:

这是我的另一个想法……还没有。

if (grade[i].equals("F"))
            pass = totalunits - Units[i];
        gpa = totalgradepoints / totalunits;

【讨论】:

    【解决方案2】:

    这是我的完整答案。唯一剩下的就是我不知道如何保留 3 位小数...当我想出一些东西时我会更新。

     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));
        }
    
    }
    

    【讨论】:

      【解决方案3】:

      虽然我猜你只会撕掉这个答案然后把它上交,但我还是写了它。重量计算取自gpacalculator.net。如果这是生产代码,会有更好的 cmets、封装等。

      注意:其中一些条目需要滚动...

      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...
          }
      }
      

      【讨论】:

      • 对不起,你的猜测是错误的。我可能是编码的初学者,我可能会(很多)在编码方面很烂——但我永远不会复制答案。我来这里是为了学习不要抄袭。如果我愿意,我会直接去 Chegg 并预先付款以获得答案。
      • 话虽如此,我非常感谢您的意见。谢谢你。我在下面粘贴了我自己的,所以你不要误解我。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-21
      • 1970-01-01
      • 2021-07-27
      • 1970-01-01
      • 2019-07-12
      • 2018-06-03
      • 1970-01-01
      相关资源
      最近更新 更多