不能在Java中使用枚举(错误:找不到符号) [英] Can't use enum in Java (Error:Can't find symbol)

查看:2226
本文介绍了不能在Java中使用枚举(错误:找不到符号)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个类文件,只有我的枚举,看起来像这样

So I have a class file with just my enums that looks like this

public class FactionNames {
    public enum Faction {AMITY, ABNIGATION, DAUNTLESS, EURIDITE, CANDOR};
}

我有一个类,在一个构造函数中使用这些枚举, / p>

I have a class that uses these enums in a constructor which looks like this

public Dauntless(String f, String l, int a,  int ag, int end, Faction d) {
        super(f, l, a, d);
        if (ag >= 0 && ag <= 10) {
            this.agility = ag;
        } else {
            this.agility = 0;
        }
        if (end >= 0 && end <= 10) {
            this.endurance = end;
        } else {
            this.endurance = 0;
        }
    }

所以要确保这个类中的所有内容都正常工作想在驱动程序中创建一些Dauntless对象,但是我不断得到这些错误。

So to make sure everything in this class works properly I want to create some Dauntless objects in a driver, but I keep getting these errors

D:\Documents\Google Drive\Homework\1331
Test.java:3: error: cannot find symbol
        Faction test;
        ^
  symbol:   class Faction
  location: class Test
Test.java:4: error: cannot find symbol
        test = Faction.DAUNTLESS;
               ^
  symbol:   variable Faction
  location: class Test
2 errors 

我正在使用我的驱动程序,看起来像这样。我的语法有什么问题吗?我不知道为什么我得到这个错误。

I'm using my driver which looks like this. Is there anything wrong with my syntax? I can't figure out why i'm getting this errors.

public class Test {
    public static void main(String[] args) {
        Faction test; 
        test = Faction.DAUNTLESS;
        Dauntless joe = new Dauntless("Joseph", "Hooper", 20, 5, 3, test);
        Dauntless vik = new Dauntless("Victoria", "Ward", 19, 6, 2, test);
        Dauntless winner;
        winner = joe.battle(vik);
        System.out.println(winner);

    }
}


推荐答案

枚举类型 Faction 嵌套在顶级类 FactionNames

The enum type Faction is nested within the top level class FactionNames.

public class FactionNames {
    public enum Faction {AMITY, ABNIGATION, DAUNTLESS, EURIDITE, CANDOR};
}

如果要使用其简单名称,您需要导入它

If you want to use its simple name, you'll need to import it

import com.example.FactionNames.Faction;

或者,您可以使用其限定名称

Alternatively, you can use its qualified name

FactionNames.Faction test = FactionNames.Faction.DAUNTLESS;;

这篇关于不能在Java中使用枚举(错误:找不到符号)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆