我希望用户输入一个1到7之间的数字。如果该数字是1,则将其打印为星期一。如果数字为2,则打印为星期二,等等。
下面的代码可以编译,但是在逻辑上是不正确的。
请参阅注释,了解需要协助的地方:
import java.util.Scanner;
public class Test {
private enum Week {
MONDAY("1"), TUESDAY("2"), WEDNESDAY("3"),
THURSDAY("4"), FRIDAY("5"), SATURDAY("6"), SUNDAY("7");
private String value;
private Week(String value) {
this.value = value;
}
}
public static void main(String[] args) {
// How to instantiate this correctly, so that it doesn't only point to one value
Week week = Week.MONDAY;
System.out.print("Enter a number: ");
Scanner input = new Scanner(System.in);
String userInput = input.next();
// The switch parameter is not correct. This is what needs to be fixed.
// I want to use this case layout.
//The enums each have a value. The user enters a value. I want to switch over userInput.
switch (week) {
case MONDAY: System.out.println("That's Monday"); break;
case TUESDAY: System.out.println("That's Tuesday"); break;
case WEDNESDAY: System.out.println("That's Wednesday"); break;
case THURSDAY: System.out.println("That's Thursday"); break;
case FRIDAY: System.out.println("That's Friday"); break;
case SATURDAY: System.out.println("That's Saturday"); break;
case SUNDAY: System.out.println("That's Sunday"); break;
default: break;
}
}
}
答案 0 :(得分:4)
在Week枚举中创建一个get方法,以便通过给定字符串获取Week,例如:
IconButton
或者您可以尝试使用Enum方法valueOf(...)。
我认为,有了这个,您有必要重新开始吗?
答案 1 :(得分:2)
这是一个版本:
import java.util.Arrays;
import java.util.Scanner;
public class Test {
private enum Week {
MONDAY(1, "That's Monday"), TUESDAY(2, "That's Tuesday"), WEDNESDAY(3, "That's Wednesday"), THURSDAY(4,
"That's Thursday"), FRIDAY(5,
"That's Friday"), SATURDAY(6, "That's Saturday"), SUNDAY(7, "That's Sunday");
private int value;
private String description;
private Week(int value, String description) {
this.value = value;
this.description = description;
}
public static Week getByValue(int value) {
return Arrays.stream(values()).findFirst().get();
}
public String getDescription() {
return description;
}
}
public static void main(String[] args) {
System.out.print("Enter a number: ");
Scanner input = new Scanner(System.in);
int userInput = input.nextInt();
Week week = Week.getByValue(userInput);
System.out.println(week.getDescription());
}
}
当输入的号码无效时,我留给您错误处理。
如您所见,我删除了switch
块,并从枚举描述中得到了消息。
答案 2 :(得分:2)
您需要创建一个Week
变量,该变量对应于用户输入的数字。
首先解析输入的字符串以获得整数,然后使用枚举类型的values()
集合获取枚举值:
public static void main(String[] args) {
System.out.print("Enter a number: ");
Scanner input = new Scanner(System.in);
String userInput = input.nextLine();
int userNumber= Integer.parseInt(userInput) % Week.values().length;
Week userWeek = Week.values()[userNumber - 1];
switch (userWeek) {
case MONDAY: System.out.println("That's Monday"); break;
case TUESDAY: System.out.println("That's Tuesday"); break;
case WEDNESDAY: System.out.println("That's Wednesday"); break;
case THURSDAY: System.out.println("That's Thursday"); break;
case FRIDAY: System.out.println("That's Friday"); break;
case SATURDAY: System.out.println("That's Saturday"); break;
case SUNDAY: System.out.println("That's Sunday"); break;
default: break;
}
}
答案 3 :(得分:1)
您需要引入一种方法,该方法根据传入的值获取枚举常量:
public static Week fetchValue(String constant) {
for (Week week : Week.values()) {
if (week.value.equals(constant)) {
return week;
}
}
return null;
}
现在像这样使用它:
Week weekday = Week.fetchValue(userInput);
if (weekday != null) {
switch (week ) {
// rest of the code
}
} else {
System.out.println("Incorrect input");
}
答案 4 :(得分:1)
Enum
已经有其整数值,但是它是从0
开始计数,因此如果可以进行较小的重新排列,则不需要单独的value
字段。
import java.util.Scanner;
public class Main {
private enum Week {
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY;
static Week get(Integer i) {
return values()[i % 7];
}
}
public static void main(String[] args) {
System.out.print("Enter a number: ");
Scanner input = new Scanner(System.in);
int userInput = input.nextInt();
Week week = Week.get(userInput);
switch (week) {
case MONDAY:
System.out.println("That's Monday");
break;
case TUESDAY:
System.out.println("That's Tuesday");
break;
case WEDNESDAY:
System.out.println("That's Wednesday");
break;
case THURSDAY:
System.out.println("That's Thursday");
break;
case FRIDAY:
System.out.println("That's Friday");
break;
case SATURDAY:
System.out.println("That's Saturday");
break;
case SUNDAY:
System.out.println("That's Sunday");
break;
default:
break;
}
}
}
答案 5 :(得分:1)
简单地使用它:
public class Test {
private enum Week {
MONDAY("1"),
TUESDAY("2"),
WEDNESDAY("3"),
THURSDAY("4"),
FRIDAY("5"),
SATURDAY("6"),
SUNDAY("7");
private String value;
private Week(String value) {
this.value = value;
}
}
public static void main(String[] args) {
// How to instantiate this correctly, so that it doesn't only point to one value
int dayOfWeek ;
System.out.print("Enter a number: ");
Scanner input = new Scanner(System.in);
dayOfWeek = Integer.valueOf(input.next()) -1;
if (dayOfWeek < 0 || dayOfWeek > 6) {
System.out.println("Out of range day of week");
System.exit(1);
}
// The switch parameter is not correct. This is what needs to be fixed.
// I want to use this case layout.
//The enums each have a value. The user enters a value. I want to switch over userInput.
switch (Arrays.asList(Week.values()).get(dayOfWeek)) {
case MONDAY: System.out.println("That's Monday"); break;
case TUESDAY: System.out.println("That's Tuesday"); break;
case WEDNESDAY: System.out.println("That's Wednesday"); break;
case THURSDAY: System.out.println("That's Thursday"); break;
case FRIDAY: System.out.println("That's Friday"); break;
case SATURDAY: System.out.println("That's Saturday"); break;
case SUNDAY: System.out.println("That's Sunday"); break;
default: break;
}
}
}
答案 6 :(得分:1)
顺便说一句,这是无需开关即可编写它的方法。
import java.util.Map;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Test {
public static void main(String[] args) {
System.out.print("Enter a number: ");
Scanner input = new Scanner(System.in);
int userInput = input.nextInt();
WeekDay wd = WeekDay.dayByValueMap.get(userInput);
if (wd == null)
System.out.println("Unknown code " + userInput);
else
System.out.println("That's " + wd.titleCase());
}
}
enum WeekDay {
MONDAY(1), TUESDAY(2), WEDNESDAY(3),
THURSDAY(4), FRIDAY(5), SATURDAY(6), SUNDAY(7);
private int value;
private WeekDay(int value) {
this.value = value;
}
final static Map<Integer, WeekDay> dayByValueMap = Stream.of(values())
.collect(Collectors.toMap(w -> w.value, w -> w));
String titleCase() {
return Character.toTitleCase(name().charAt(0)) + name().substring(1).toLowerCase();
}
}
答案 7 :(得分:0)
这里是一个例子:
enum Week{
MONDAY("1");
private String value;
Week(String value) {
this.value = value;
}
public String getValue() {
return value;
}
public static Week getInstance(String value) {
return Week.valueOf(value);
}
}
public static void main(String[] args) {
String a = "1";
Week w = Week.getInstance(a);
switch (w) {
case MONDAY:
System.out.println("It is monday");
break;
default:
break;
}
}