1

我是 Java 新手,正在为一个简单的程序编写一些代码。

该程序将询问用户他们上过多少节课,然后询问每节课的字母等级和学分。

我在编写这个程序时遇到了很多麻烦。我想我已经接近了,但我仍然在为一些事情苦苦挣扎。具体来说,我收到了“精度损失”错误,但我的印象是这些错误仅在使用 Math.pow 或浮点数时才会出现。

这是我所拥有的:

导入 java.util.Scanner;公共课GPA

{
  public static void main(String[] args)
   {

    Scanner kbReader = new Scanner(System.in);

    System.out.println("Enter number of classes: ");
    int numberClasses = kbReader.nextInt();

    int i;

      for (i = 0; i < numberClasses; i++)
      {

  System.out.print("Enter letter grade of class: ");
      int letterGrade = kbReader.nextInt();

  System.out.print("Enter credit hour of class: ");
  int creditHour = kbReader.nextInt();

int totalCreditForGPA;
int totalCreditHours;

switch (letterGrade)
    {
   case 'A':
   case 'a': totalCreditForGPA = totalCreditForGPA + (creditHour * 4.0);
         totalCreditHours =  totalCreditHours + creditHour;
         break;
  case 'B':
  case 'b':  totalCreditForGPA = totalCreditForGPA + (creditHour * 3.0);
         totalCreditHours =  totalCreditHours + creditHour;
         break;
  case 'C':
  case 'c':  totalCreditForGPA = totalCreditForGPA + (creditHour * 2.0);
         totalCreditHours =  totalCreditHours + creditHour;
         break;
  case 'D':
  case 'd':  totalCreditForGPA = totalCreditForGPA + (creditHour * 1.0);
         totalCreditHours =  totalCreditHours + creditHour;
         break;
  case 'F':
  case 'f':  totalCreditForGPA = totalCreditForGPA + (creditHour * 0.0);
         totalCreditHours =  totalCreditHours + creditHour;
         break;
  default:
         System.out.println("Invalid letter grade.");
    }
  }

int GPA = totalCreditforGPA;
System.out.println("Your GPA is " + GPA);
   }
}

感谢您的帮助。

4

2 回答 2

1

这些行会导致您丢失精度问题

totalCreditForGPA = totalCreditForGPA + (creditHour * 4.0);
totalCreditHours =  totalCreditHours + creditHour;

你将一个 int 乘以一个 double,它向上转换为一个 double,你添加到一个 int 仍然保持它是一个 double,它不能存储在一个 int 变量中。解决方案是将变量存储为双精度数,或乘以整数(例如,4 而不是 4.0)。记住要在任何你乘以双精度的地方解决问题,这意味着在你的 switch 语句的所有情况下。

于 2013-07-14T19:13:26.677 回答
1

您的代码中有两个问题:
1.您正在循环外访问循环局部变量。
2.您将 double 结果转换为 int 是不可能的,您必须使用显式转换或使用 double 类型以防止精度损失。

     package com.general.generics;
    import java.util.Scanner; 
    public class GPA

    {
      public static void main(String[] args)

   {

    Scanner kbReader = new Scanner(System.in);

    System.out.println("Enter number of classes: ");
    int numberClasses = kbReader.nextInt();

    int i;
    int totalCreditForGPA=0; //these should be defined outside loop and should be double for precision.
    int totalCreditHours=0;
      for (i = 0; i < numberClasses; i++)
      {

      System.out.print("Enter letter grade of class: ");
          int letterGrade = kbReader.nextInt();

      System.out.print("Enter credit hour of class: ");
      int creditHour = kbReader.nextInt();



    switch (letterGrade)
        {
       case 'A':
       case 'a': totalCreditForGPA = (int) (totalCreditForGPA + (creditHour * 4.0)); //result should be type to int or use totalCreditForGPA as double
             totalCreditHours =  totalCreditHours + creditHour;
             break;
      case 'B':
      case 'b':  totalCreditForGPA = (int) (totalCreditForGPA + (creditHour * 3.0));
             totalCreditHours =  totalCreditHours + creditHour;
             break;
      case 'C':
      case 'c':  totalCreditForGPA = (int) (totalCreditForGPA + (creditHour * 2.0));
             totalCreditHours =  totalCreditHours + creditHour;
             break;
      case 'D':
      case 'd':  totalCreditForGPA = (int) (totalCreditForGPA + (creditHour * 1.0));
             totalCreditHours =  totalCreditHours + creditHour;
             break;
      case 'F':
      case 'f':  totalCreditForGPA = (int) (totalCreditForGPA + (creditHour * 0.0));
             totalCreditHours =  totalCreditHours + creditHour;
             break;
      default:
             System.out.println("Invalid letter grade.");
        }
      }

    int GPA = totalCreditForGPA;
    System.out.println("Your GPA is " + GPA);
       }
    }
于 2013-07-14T19:16:26.743 回答