【发布时间】:2021-10-09 23:48:08
【问题描述】:
我已经对代码进行了更改,并且能够成功我希望将 switch case 与字符串一起使用。现在我只需要帮助为字符串 getDirection 声明两个变量。我需要将其声明为“右”和“左”
String quit= "exit";
String choice;
String getDirection= "Right";//I need it to be declared as both left and right
quit.equalsIgnoreCase("exit");
do
{
System.out.println("would you like the bug to move or turn around?");
System.out.println();
System.out.println("Or enter 'exit' to quit");
choice= scan.nextLine();
option options=option.MOVE;
switch(options)
{
case MOVE:
b.move();
if (getDirection.equals("Right"))
System.out.println("The bug moved 1 unit towards " + b.getDirection("Right ") + " and is now at position " + b.getPosition());
else if (getDirection.equals("Left"))
System.out.println("The bug moved 1 unit towards " + b.getDirection("Left ") + " and is now at position " + b.getPosition());
【问题讨论】:
-
将枚举值命名为大写是标准做法,如
MOVE、TURN。 -
而且,通常最好用单数而不是复数来命名枚举类。
-
谢谢,我已经更正了@chrylis-cautiouslyoptimistic-
-
这不可能是真的:
if (d.equals("Right") & d.equals("Left")) -
这里的字符串参数没有用:
public String getDirection(String dir)//gives the direction the bug is faced
标签: java string enums switch-statement