class UseEnums2 {
enum Colors {
RED, GREEN, BLUE, YELLOW
}; public static void main(String[] args) {
for (Colors c : Colors.values()) {
if (c == Colors.GREEN)
System.out.print("green ");
if (Colors.RED.equals(c))
System.out.print("red ");
if (c == "YELLOW")
System.out.print("yellow ");
if (c.equals("BLUE"))
System.out.print("blue ");
}
}
}

解决方案 »

  1.   

    c == "YELLOW"
    比较对象类型不同
      

  2.   

    enum类型的变量值是int ,而你的要比较的是Corlor类型的,当然出错了.
     
    在枚举中如果没有对变量进行显式说明,默认的第一个变量的值是0,往后加1.
      

  3.   

    因为c是colors  red是color
      

  4.   

    equals就不用考虑比较类型了吗?
      

  5.   

    equals重写的是Object中定义的方法
    public boolean equals(Object obj);
    参数为Object类型,所以即使你传错了,编译器也不会检查出问题。
      

  6.   

    C是一个变量,,而“YELLOW”是一个字符串
    class UseEnums2 {
        enum Colors {
            RED, GREEN, BLUE, YELLOW
        };    public static void main(String[] args) {
            for (Colors c : Colors.values()) {
                if (c == Colors.GREEN)
                    System.out.print("green ");
                if (Colors.RED.equals(c))
                    System.out.print("red ");
                if (c == Colors.YELLOW)
                    System.out.print("yellow ");
                if (c.equals("BLUE"))
                    System.out.print("blue ");
            }
        }
    }red green yellow 
    Process completed.
      

  7.   

    那既然"=="是比较地址的,c和"YELLOW"的地址不同,为什么不是false?
      

  8.   

    对不起,粘贴错代码了。==号是有错。
    因为类型不兼容嘛。
    错误信息显示:不能将UseEnums2.Colors与一个String进行比较