質問

enum key{
   ABCD,XYZ
}

public class example {
     public static void main(String args[])
     {
       String Pass,a;
       Scanner in = new Scanner(System.in);
       System.out.println("enter the password");
       Pass=in.nextLine();
         if(Pass==key.ABCD)
            {
             System.out.println("your password was correct"+key.ABCD);
            }
         else
             {
             System.out.println("your password was incorrect");
             }
        }
}
役に立ちましたか?

解決 2

Replace if(Pass==key.ABCD) with if(key.valueOf(Pass)==key.ABCD).

Or,

Since you have arbitory inputs values to check, it's better to convert the enum value to String and compare it. So change the if condition to if(Pass.equals(key.ABCD.name()))

他のヒント

The string Pass should be converted to an enum of type key first, so change the if part to :

if(key.valueOf(Pass) == key.ABCD){
   ...
}
enum key {
    ABCD, XYZ;

    public static key getKeyFromString(String keyStr) {
        key rtnKey = null;
        if (keyStr != null) {
            for (key value : values()) {
                if (keyStr.equalsIgnoreCase(value.toString())) {
                    rtnKey = value;
                    break;

                }
            }
        }
        return rtnKey;

    }
}

..................................In main method.................

key userKey = key.getKeyFromString(Pass)

user usrKey for comparison

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top