JAVA 开关,if then else 和带有字符串的布尔值
JAVA Switches, if then else and booleans with string
我在让部分代码正常工作时遇到了一些麻烦。
我对 java 还是有点陌生,能否提供一些指导和线索来指出我哪里出错了。
错误来自 if 语句。我觉得我知道他们为什么会犯错,因为 ||未定义,但我不确定如何修复它。我想让它做的是接受 L、R、F、B(左、右、前和后)的输入。小写输入并使用布尔值 "or".
接受一个或另一个
import java.util.Scanner;
public class 选择你的冒险 {
public static void main(String[]args) {
Scanner input = new Scanner(System.in);
System.out.print("Choose a diection: ");
String direction = input.nextLine().toLowerCase();
System.out.printf(" %s and %s/n",getDirection (way),getYourChoice (found));
}
public static String getYourChoice (String found) {
String result = "Unknown";
switch (found)
{
case "l":
result = " now we all know you can turn left unlike Zoolander";
break;
case "left":
result = " now we all know you can turn left unlike Zoolander";
break;
case "r":
result = " you fall down a hole never to be seen again... sad.";
break;
case "right":
result = " you fall down a hole never to be seen again... sad.";
break;
case "f":
result = " YOU ARE THE KWISATZ HADERACH!!";
break;
case "forward":
result = " YOU ARE THE KWISATZ HADERACH!!";
break;
case "b":
result = " you are a scaredy cat but, you live to fight or runaway another day";
break;
case "back":
result = " you are a scaredy cat but, you live to fight or runaway another day";
break;
}
return result;
}
public static String getDirection(String way) {
String result;
if (way == "l" || "left") {
System.out.print("Your character moves left");
}
else if (way == "r" || "right") {
System.out.println("You character moves right");
}
else if (way == "f" || "forward") {
System.out.println("Your character moves forward");
}
else if (way == "b" || "back") {
System.out.println("Your character moves forward");
}
else {
System.out.println(" You cant go that way ");
}
return result;
}
}
你所有的if
陈述都是错误的。使用||
或&&
时,需要在||
两边指定变量way
:
if (way == "l" || way == "left") {
System.out.print("Your character moves left");
}
我在让部分代码正常工作时遇到了一些麻烦。 我对 java 还是有点陌生,能否提供一些指导和线索来指出我哪里出错了。
错误来自 if 语句。我觉得我知道他们为什么会犯错,因为 ||未定义,但我不确定如何修复它。我想让它做的是接受 L、R、F、B(左、右、前和后)的输入。小写输入并使用布尔值 "or".
接受一个或另一个import java.util.Scanner;
public class 选择你的冒险 {
public static void main(String[]args) {
Scanner input = new Scanner(System.in);
System.out.print("Choose a diection: ");
String direction = input.nextLine().toLowerCase();
System.out.printf(" %s and %s/n",getDirection (way),getYourChoice (found));
}
public static String getYourChoice (String found) {
String result = "Unknown";
switch (found)
{
case "l":
result = " now we all know you can turn left unlike Zoolander";
break;
case "left":
result = " now we all know you can turn left unlike Zoolander";
break;
case "r":
result = " you fall down a hole never to be seen again... sad.";
break;
case "right":
result = " you fall down a hole never to be seen again... sad.";
break;
case "f":
result = " YOU ARE THE KWISATZ HADERACH!!";
break;
case "forward":
result = " YOU ARE THE KWISATZ HADERACH!!";
break;
case "b":
result = " you are a scaredy cat but, you live to fight or runaway another day";
break;
case "back":
result = " you are a scaredy cat but, you live to fight or runaway another day";
break;
}
return result;
}
public static String getDirection(String way) {
String result;
if (way == "l" || "left") {
System.out.print("Your character moves left");
}
else if (way == "r" || "right") {
System.out.println("You character moves right");
}
else if (way == "f" || "forward") {
System.out.println("Your character moves forward");
}
else if (way == "b" || "back") {
System.out.println("Your character moves forward");
}
else {
System.out.println(" You cant go that way ");
}
return result;
}
}
你所有的if
陈述都是错误的。使用||
或&&
时,需要在||
两边指定变量way
:
if (way == "l" || way == "left") {
System.out.print("Your character moves left");
}