[英]Java: Incompatible Types
import java.text.*;
import java.util.*;
public class Proj3 {
public static void main(String[]args){
// DecimalFormat df = new DecimalFormat("#0.00”);
Scanner s = new Scanner(System.in);
int TotalHours = 0;
int TotalGrade = 0;
System.out.print("How many courses did you take? ");
int Courses = Integer.parseInt(s.nextLine());
System.out.println("");
int CourseNumber = Courses - (Courses - 1);
while (Courses > 0){
System.out.print("Course (" + CourseNumber +"): How many hours? ");
int Hours = Integer.parseInt(s.nextLine());
TotalHours = TotalHours + Hours;
System.out.print("Course (" + CourseNumber +"): Letter grade? ");
char Grade = s.nextLine().charAt(0);
if (Grade == 'A'){
TotalGrade = TotalGrade + (4 * Hours);
}
if (Grade == 'B'){
TotalGrade = TotalGrade + (3 * Hours);
}
if (Grade == 'C'){
TotalGrade = TotalGrade + (2 * Hours);
}
if (Grade == 'D'){
TotalGrade = TotalGrade + (1 * Hours);
}
Courses = Courses - 1;
CourseNumber = CourseNumber + 1;
}
Double GPA = TotalGrade / TotalHours;
System.out.println(df.format(GPA));
}
}
This is for an assignment and I don't know how to fix my problem. 这是一项任务,我不知道如何解决我的问题。 The Double GPA = TotalGrade / ToutalHours;
Double GPA = TotalGrade / ToutalHours;
line is coming up with the Incompatible Types
error. 行出现“ Incompatible Types
错误。
Also I'm supposed to include the DecimalFormat df = new DecimalFormat("#0.00”);
line at the beginning of the main but its not working. 另外,我应该在主程序的开头包含DecimalFormat df = new DecimalFormat("#0.00”);
行,但该行不起作用。
Anything is very helpful. 一切都非常有帮助。 Thanks 谢谢
Double GPA = TotalGrade / TotalHours;
Autoboxing
is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. Autoboxing
是Java编译器在原始类型及其对应的对象包装器类之间进行的自动转换。 Hence Double
is wrapper class for primitive double
and Integer
is wrapper class for primitive int
. 因此, Double
是原始double
包装器类, Integer
是原始int
包装器类。
Your TotalGrade / TotalHours
is resulting in an int
but you are trying to assign it to object with type Double
. 您的TotalGrade / TotalHours
产生一个int
但您试图将其分配给Double
类型的对象。 Do either: 执行以下任一操作:
double GPA = (double)TotalGrade / TotalHours;
or, 要么,
Double GPA = (double)TotalGrade / TotalHours;
You are using a wrapper class Double
. 您正在使用包装器类Double
。 While your variables TotalGrade / ToutalHours
are primitive types int. 虽然您的变量TotalGrade / ToutalHours
是基本类型int。 Hence they are incompatible. 因此它们是不兼容的。 Using primitive double will solve this issue. 使用原始double将解决此问题。
double GPA = TotalGrade / ToutalHours;
Same for DecimalFormat
issue, the format
method doesn't accept wrapper Double
. 与DecimalFormat
问题相同, format
方法不接受包装器Double
。 So when you use primitive double
it will solve the issue. 因此,当您使用原始double
,它将解决此问题。
There are two problems in your code 您的代码中有两个问题
change your Double GPA = TotalGrade / TotalHours; 更改您的Double GPA = TotalGrade / TotalHours;
to Double GPA = (double)TotalGrade / TotalHours; GPA的两倍=(double)TotalGrade / TotalHours; //this is explicit type casting //这是显式类型转换
or double GPA = TotalGrade/ TotalHours // this type promotion 或两倍GPA = TotalGrade / TotalHours //此类型的促销
Second problem is 第二个问题是
// DecimalFormat df = new DecimalFormat("#0.00”);
in this line see the closing quotes its wrong change it to "
在此行中,请以右引号将其错误地更改为"
You are trying to convert int To Wrapper class Double. 您正在尝试将int转换为Wrapper类Double。
Replace 更换
Double GPA = TotalGrade / TotalHours;
With 用
double GPA = TotalGrade /TotalHours;
AND you can write 而且你可以写
NumberFormat formatter = new DecimalFormat("#0.000");
At begining of main if you want and print it like 如果需要,可以在main开始时像这样打印
System.out.println("The Decimal Value is:"+formatter.format(GPA));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.