我的主 If 语句要求输入。如果是“二月”,则进入下一步。我希望在我的主要 elseif 和 else 完成自己的部分之后执行相同的步骤。 我还想将“二月”转换为“allUppercase”或“alllowerCase”以使其不敏感。但是当我编写“”if(s1.toLowerCase(“二月”))“”时出现错误 谢谢
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Compare {
public static void main(String[] args) {
String[] months1 = {"january","march","april","may","june","july","augest","september","october","november","december"};
String s1 = getInput("Enter a month ");
if(s1.equals("february")) {
System.out.println("It's time to go to the Disneyland !");
String s2 = getInput("Enter another month: ");
if(s2.equals("february")) {
System.out.println("You already won a Disneyland ticket! Try another month.");
String s3 = getInput("Enter another month: ");
String[] months = {"january","march","april","may","june","july","augest","september","october","november","december"};
if(Arrays.asList(months).contains(s3.toLowerCase())) {
System.out.println("You will go to Paris");
}else{
String s4 = getInput("Leave your name and phone number. We will call you back. ");
System.out.println("Thanks for visiting! Goodbye !" + s4);
}
}
}else if(Arrays.asList(months1).contains(s1.toLowerCase())){
System.out.print("Sorry we don't have any specials yet");
}else{
System.out.print("Phahaha:))) choose sthg else");
}
}
private static String getInput(String prompt) {
BufferedReader stdin = new BufferedReader(
new InputStreamReader(System.in));
System.out.print(prompt);
System.out.flush();
try {
return stdin.readLine();
} catch (Exception e) {
return "Error: " + e.getMessage();
}
}
}
最佳答案
确实是 if(s1.toLowerCase("february"))
是一个编译错误,因为 s1.toLowerCase("february")
返回一个字符串并且 if ()
需要一个 boolean 值。
使用 if(s1.equalsIgnoreCase("february"))
` 比较字符串忽略大小写
关于java - 如何使我的 main if、elseIf 和 else 执行与我的 main if 相同的操作而不出现 "duplicate"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31233404/