java - 如何使我的 main if、elseIf 和 else 执行与我的 main if 相同的操作而不出现 "duplicate"错误?

标签 java

我的主 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/

相关文章:

java - Spring 数据 JPA : How to write subquery having IN operator

java - 如何使我的 JSP 项目更易于设计人员使用

java - Hibernate Validator 检查数组大小

java - Android编程——制作URI获取音频位置

java - 一旦我从 gwt 中的一个选项卡注销,如何从所有打开的选项卡注销

java - 需要 Java 中文件的内容创建日期

java - 由于负载平衡,全局变量未更新

java - 应该在 java.util.Scanner 中使用什么模式来获取下一个字符串标识符?

java - 从表中删除列表中不存在 id 的记录,保留现有记录并插入表中不存在的记录

java - Nullable @ManyToOne 不接受 null 值