失去精度误差

时间:2013-07-14 19:09:25

标签: java switch-statement precision

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

该程序将询问用户他们已经上了多少课程,然后是每个班级的一个字母等级和学分。

我在编写这个程序时遇到了很多麻烦。我想我已经把它关闭了,但我仍然在努力解决一些问题。具体来说,我被给予“精度损失”错误,但我的印象是那些只在使用Math.pow或浮动时出现。

这就是我所拥有的:

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

感谢您在advace的帮助。

2 个答案:

答案 0 :(得分:1)

这些行导致精度问题丢失

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

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

答案 1 :(得分:1)

您的代码中存在两个问题:
    1.您正在循环外访问循环局部变量 2.你将双结果转换为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);
       }
    }