请帮助找到我的代码所在的位置"不兼容的类型" ....我看了看,看了看,无法在任何地方找到它。
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.
}
}
答案 0 :(得分:3)
System.out.println(
years = "y =" + y.getCenturies() + "c +" + y.getDecades() + "d"
);
// ^^^^^^^
问题是years =
。编译器并不确定如何处理这个问题。 =
右侧的结果是一个String,因为您正在执行字符串连接。
所以编译器认为你这样做:
years = ("y =" + y.getCenturies() + "c +" + y.getDecades() + "d")
years
是一个int,所以这不是一个有效的表达式。
你可能只是这个意思:
System.out.println(
"y = " + y.getCenturies() + "c + " + y.getDecades() + "d"
);
或者可能在那里的某处连接years
:
System.out.println(
years + "y = " +
y.getCenturies() + "c + " + y.getDecades() + "d"
);