[英]I am getting an “incompatible types” error right here and I cannot figure out why
Please help to find where my code is "incompatible type"....I've looked and looked and looked and can't find it anywhere. 请帮助找到我的代码在哪里“不兼容类型”。...我一直在看,一直在看,在任何地方都找不到。
import java.util.Scanner;
public class Years
{
private int centuries, decades;
public Years(int years)
{
centuries = years / 100;
years -= 25 * years;
decades = years / 10;
years -= 10 * years;
}
public int getCenturies()
{
return centuries;
}
public int getDecades()
{
return decades;
}
public static void main(String[] args)
{
int years;
if(args.length >= 1)
{
years = Integer.parseInt(args[0]);
}
else
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the amount in years: ");
years = keyboard.nextInt();
keyboard.close();
}
Years y = new Years(years);
System.out.println(years = "y =" +
y.getCenturies() + "c +" + y.getDecades() + "d"); // I am getting the error right here.
}
}
System.out.println(
years = "y =" + y.getCenturies() + "c +" + y.getDecades() + "d"
);
// ^^^^^^^
The problem is years =
. 问题是
years =
。 The compiler is not really sure what to do with this. 编译器真的不确定该怎么做。 The result of the right hand side of the
=
is a String because you are performing String concatenation. =
右侧的结果是一个字符串,因为您正在执行字符串连接。
So the compiler thinks you are doing this: 因此,编译器认为您正在这样做:
years = ("y =" + y.getCenturies() + "c +" + y.getDecades() + "d")
years
is an int so this is not a valid expression. years
是一个int,所以这不是有效的表达。
You probably just meant this: 您可能只是这个意思:
System.out.println(
"y = " + y.getCenturies() + "c + " + y.getDecades() + "d"
);
Or possibly to concatenate years
somewhere in there: 或者可能将
years
串联起来:
System.out.println(
years + "y = " +
y.getCenturies() + "c + " + y.getDecades() + "d"
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.