我是 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);
}
}
感谢您的帮助。