gpt4 book ai didi

java - 精度损失误差

转载 作者:行者123 更新时间:2023-11-30 04:18:51 24 4
gpt4 key购买 nike

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

该程序将询问用户他们已经选修了多少类(class),然后询问每门类(class)的字母等级和学分。

我在编写这个程序时遇到了很多麻烦..我想我已经接近了,但我仍然在努力解决一些问题。具体来说,我遇到了“精度损失”错误,但我的印象是这些错误仅在使用 Math.pow 或 float 时才会出现。

这是我得到的:

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

感谢您的提前帮助。

最佳答案

您的代码中有两个问题:
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);
}
}

关于java - 精度损失误差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17642719/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com