简体   繁体   English

我在这里收到“不兼容的类型”错误,我不知道为什么

[英]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.

相关问题 无法弄清楚为什么我得到不兼容的类型 - Cannot figure out why I am getting incompatible types 为什么我收到“不兼容类型错误”? - Why I am getting `incompatible types error`? 为什么我收到不兼容的类型错误? - Why am i getting incompatible types error? 为什么我会得到“不兼容的类型:对象无法转换为字符串”? - Why am I getting, “incompatible types: Object cannot be converted to String” with this? 为什么会出现错误:不兼容的类型; JButton无法转换为JTextfield - Why am I getting the error: Incompatible types; JButton cannot be converted to a JTextfield 我不知道类型不兼容的错误 - incompatible types error i can't figure out 为什么我会在我的SQLiteOpenHelper构造函数中获得“不兼容的类型:Bla无法转换为Context”? - Why am I getting, “incompatible types: Bla cannot be converted to Context” in my SQLiteOpenHelper constructor? 我尝试测试的Java类在NetBeans“不兼容的类型:int无法转换为Date”中出现错误 - Getting error in NetBeans “incompatible types: int cannot be converted to Date” for Java class I am trying to test 在android中编程时出现错误:不兼容的类型:InboxStye无法转换为Style - I am getting an error while programming in android: incompatible types : InboxStye cannot be converted into Style 为什么我从Java编译器中获得不兼容的类型? - Why am I getting an incompatible types from the java compiler?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM