Java:不兼容的类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19602483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: Incompatible Types
提问by user2922081
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;
line is coming up with the Incompatible Types
error.
这是一个任务,我不知道如何解决我的问题。该Double GPA = TotalGrade / ToutalHours;
行出现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
任何事情都非常有帮助。谢谢
回答by Sage
Double GPA = TotalGrade / TotalHours;
Autoboxing
is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. Hence Double
is wrapper class for primitive double
and Integer
is wrapper class for primitive int
.
Autoboxing
是 Java 编译器在原始类型与其对应的对象包装类之间进行的自动转换。因此Double
为原始包装类double
和Integer
是包装类原语int
。
Your TotalGrade / TotalHours
is resulting in an int
but you are trying to assign it to object with type Double
. Do either:
您TotalGrade / TotalHours
的结果是 aint
但您正试图将其分配给 type 的对象Double
。执行以下任一操作:
double GPA = (double)TotalGrade / TotalHours;
or,
或者,
Double GPA = (double)TotalGrade / TotalHours;
回答by Gaurav Varma
You are using a wrapper class Double
. While your variables TotalGrade / ToutalHours
are primitive types int. Hence they are incompatible. Using primitive double will solve this issue.
您正在使用包装类Double
。虽然您的变量TotalGrade / ToutalHours
是原始类型 int。因此它们是不相容的。使用原始 double 将解决这个问题。
double GPA = TotalGrade / ToutalHours;
Same for DecimalFormat
issue, the format
method doesn't accept wrapper Double
. So when you use primitive double
it will solve the issue.
DecimalFormat
问题相同,该format
方法不接受 wrapper Double
。因此,当您使用原语时double
,它将解决问题。
回答by Prashant Shilimkar
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));
回答by ankit
There are two problems in your code
你的代码有两个问题
- Java allows type promotion but you are trying to promote an int value to Double which is not possible this can be resolved in following two way
- Java 允许类型提升,但您试图将 int 值提升为 Double 这是不可能的,这可以通过以下两种方式解决
change your Double GPA = TotalGrade / TotalHours;
to Double GPA = (double)TotalGrade / TotalHours; //this is explicit type casting
or double GPA = TotalGrade/ TotalHours // this type promotion
改变你的双 GPA = TotalGrade / TotalHours;
双 GPA = (double)TotalGrade / TotalHours; //这是显式类型转换
或双 GPA = TotalGrade/ TotalHours // 这种类型的促销
Second problem is
第二个问题是
// DecimalFormat df = new DecimalFormat("#0.00”);
in this line see the closing quotes its wrong change it to "
在这一行看到结束引号它的错误将其更改为 "