Java和带有扫描仪的枚举

Java和带有扫描仪的枚举

本文介绍了Java和带有扫描仪的枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个枚举问题。我有一个类 Type ,其中有两个枚举: RIGHT LEFT
在另一个类中,我有一个对象 Type ,名为 value
我有一个循环:

I have a problem with an enum. I have a class Type where I have two enums: RIGHT and LEFT.In other class I have object of class Type, named value.I have a loop:

if (currentLvl.verify(value)) {
    totalPoint++;
}

其中 currentLvl.verify(value)是一种验证值是否正确的方法。我的问题是如何从用户获得。我希望用户在循环之前写他的值,然后在 value 是正确的情况下写 totalPoint ++ 。我想使用扫描仪,但不能执行 value = Scanner.nextInt(); 。如何从用户及其值归因变量 value 中获取 value

where currentLvl.verify(value) is a method which verifies if value is correct. My problem is how I can get value from user. I want the user, before loop if, to write his value and after that if value is correct, totalPoint++. I want use a scanner but I can't do value = scanner.nextInt();. How I can get value from the user and his value imput to variable value?

推荐答案

听起来像您需要使用 valueOf()方法将String转换为枚举,如下所示:

Sounds like you need to use the valueOf() method to convert the String to the enum, something like this:

Type value = Type.valueOf(scanner.next().toUpperCase());

或者如果用户输入枚举值,则使用 values() 数组

Or if the user is entering the enum value then use the values() array

Type value = Type.values()[scanner.nextInt()];

这篇关于Java和带有扫描仪的枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 16:04