gpt4 book ai didi

java - 如何将枚举与 switch 语句一起使用?

转载 作者:行者123 更新时间:2023-12-02 03:52:52 26 4
gpt4 key购买 nike

所以我查看了网站上的一些示例,但没有找到任何需要的东西。我想做的练习是将枚举类链接到页面底部的 switch 语句。

这个小部分的练习题是:“talk 方法应该要求用户输入一个短语,该短语应该打印在控制台上。娱乐机器人可以在四个方向(向前、向后、向左、向右)之一行走任意步数。因此,步行方法应该询问用户他们希望机器人走多少步以及朝哪个方向走。请注意,您必须验证用户的输入。说明必须存储为枚举并适当访问。”

枚举类:

package robot;

public enum Directions {
FORWARD, BACKWARD, LEFT, RIGHT;
}

主类:

package robot;

import java.util.Scanner;

public class EntertainmentRobot extends Robot implements Walk, Talkable {

private Directions direction;

public EntertainmentRobot(double height, double weight, String manufacturer, String name) {
super(height, weight, manufacturer, name);
setEnergy(10);
setEnergyUnitsRequired(0.75);
}

// ACCESSOR METHOD
public Directions getDirection() {
return direction;
}

// MUTATOR METHOD
public void setDirection(Directions direction) {
this.direction = direction;
}

@Override
public void start() {
System.out.println("-----I AM THE ENTERAINIMENT ROBOT-----");
}

@Override
public void stop() {
System.out.println("I have stopped entertaining people.");
}

@Override
public void doTask() {
// TODO Auto-generated method stub
}

@Override
public void doTask(Scanner in) {
System.out.println("I am entertaining people.");
System.out.println("How many people would you like to play with me?");
int times = in.nextInt();
for (int i = 0; i < times; i++) {
play();
}
System.out.println(" ");
}

public String toString() {
return "[Name= " + getName() + "\nWeight= " + getWeight() + "\nHeight:" + getHeight() + "\nManufacturer: "
+ getManufacturer() + "\nPurpose: " + getPurpose() + "]";
}

public void play() {
if (getEnergy() >= energyRequired()) {
energyComsumption();
} else {
System.out.println("\n-----WHOOPS-----");
System.out.println("I do not have enough energy to study");
regenerate();
System.out.println("I must get more energy");
System.out.println("I have regenerated my energy");
}
}

@Override
public void talk(Scanner in) {
System.out.println("Please enter a phrase for me to speak: ");
String talk = in.nextLine();
System.out.println("You asked me to say the following phrase: " + talk);
}

@Override
public void walk(Scanner in) {
System.out.println("Would you like me to walk? [YES/NO]");
String walk = in.nextLine();
if (walk.equalsIgnoreCase("Yes")) {
}
}

public void directionChoice(Scanner in) {
System.out.println("For how many paces?");
int paces = in.nextInt();
System.out.println("1 - FORWARD" + "2 - BACKWARD" + "3 - LEFT" + "4 - RIGHT");
switch (paces) {
case 1:
break;
case 2:
}
}
}

最佳答案

下面是如何在 switch 情况下使用 Direction 枚举的示例:

Direction direction = //get direction input
switch(direction){
case BACKWARD:
//do something
break;

case FORWARD:
//do something
break;

case LEFT:
//do something
break;

case RIGHT:
//do something
break;
}

但是,在执行这个开关之前,我们需要获取这个枚举的值,我猜它是用户输入(字符串)。我们可以使用枚举的valueOf()方法来获取实际值。请注意,如果输入不是值之一,valueOf() 会抛出 IllegalArgumentException,因此我们可以执行以下操作来验证用户输入:

String input = //get user's input
Direction direction;
try{
direction = Direction.valueOf(input);
}catch(IllegalArgumentException iae){
//Invalid input
}

关于java - 如何将枚举与 switch 语句一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35735422/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com