有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我希望输入不区分大小写

例如,当用户键入“9月”、“9月”、“9月”、“9月”或其他任何形式时,只要字母正确,它将是一个有效条目。写这段代码(我使用“| |”的部分)的更简洁的方法是什么

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Compare {

    public static void main(String[] args) {
        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: ");
                  if(s3.equals("January") || 
                     s3.equals("March") || 
                     s3.equals("April") || 
                     s3.equals("July") || 
                     s3.equals("May") || 
                     s3.equals("September") || 
                     s3.equals("October") || 
                     s3.equals("November") || 
                     s3.equals("Aguest") || 
                     s3.equals("July") || 
                     s3.equals("December")) {
                     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 !");
                   }    
              }
         }
     }
    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();
          }

     }

}

共 (6) 个答案

  1. # 1 楼答案

    对于大小写不敏感的问题,您应该考虑使用String对象提供的equalsIgnoreCase方法

    至于代码风格和优雅,我会将if语句封装到一个方法中,以提高可读性

    public static void main(String[] args) {
            String s1 = getInput("Enter a month ");
            if(s1.equalsIgnoreCase("February")) {
                System.out.println("It's time to go to the Disneyland !");
                String s2 = getInput("Enter another month: ");
                if(s2.equalsIgnoreCase("February")) {
                    System.out.println("You already won a Disneyland ticket! Try       another month.");
                    String s3 = getInput("Enter another month: ");
                      if(isValidMonth(s3)) {
                         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 !");
                       }    
                  }
             }
         }
    
    public static boolean isValidMonth(String s3) {
        if(s3.equalsIgnoreCase("January") || 
                     s3.equalsIgnoreCase("March") || 
                     s3.equalsIgnoreCase("April") || 
                     s3.equalsIgnoreCase("July") || 
                     s3.equalsIgnoreCase("May") || 
                     s3.equalsIgnoreCase("September") || 
                     s3.equalsIgnoreCase("October") || 
                     s3.equalsIgnoreCase("November") || 
                     s3.equalsIgnoreCase("Aguest") || 
                     s3.equalsIgnoreCase("July") || 
                     s3.equalsIgnoreCase("December")) {
            return true;
        } else {
            return false;
        }
    }
    
  2. # 2 楼答案

    <>你也可以考虑把你所有的月份都排成一个数组,做一个简单的比较:

    String[] months = {"january", "march", "april", ...};
    if (Arrays.asList(months).contains(s3.toLowerCase())
             System.out.println("You will go to Paris");
    

    您可以在线构造数组,从而节省一些糟糕的java样板

  3. # 3 楼答案

    // magic is in s3.equalsIgnoreCase
        import java.io.BufferedReader;
        import java.io.InputStreamReader;
    
        public class Compare {
    
            public static void main(String[] args) {
                String s1 = getInput("Enter a month ");
                if(s1.equalsIgnoreCase("February")) {
                    System.out.println("It's time to go to the Disneyland !");
                    String s2 = getInput("Enter another month: ");
                    if(s2.equalsIgnoreCase("February")) {
                        System.out.println("You already won a Disneyland ticket! Try       another month.");
                        String s3 = getInput("Enter another month: ");
                          if(s3.equalsIgnoreCase("January") || 
                             s3.equalsIgnoreCase("March") || 
                             s3.equalsIgnoreCase("April") || 
                             s3.equalsIgnoreCase("July") || 
                             s3.equalsIgnoreCase("May") || 
                             s3.equalsIgnoreCase("September") || 
                             s3.equalsIgnoreCase("October") || 
                             s3.equalsIgnoreCase("November") || 
                             s3.equalsIgnoreCase("Aguest") || 
                             s3.equalsIgnoreCase("July") || 
                             s3.equalsIgnoreCase("December")) {
                             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 !");
                           }    
                      }
                 }
             }
            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();
                  }
    
             }
    
        }
    
  4. # 4 楼答案

    将所有月份名称添加到列表(全部小写)并调用list.contains(input.toLowerCase())怎么样?这将使您的“| |”部分更具可读性

  5. # 5 楼答案

    使用String.equalsIgnoreCase(),它会不敏感地检查字符串大小写是否相等。[Documentation]

  6. # 6 楼答案

    对于第一个问题,在person键入月份后,可以键入s2 = s2.toLower(),然后将if语句中的所有月份设置为小写。至于另一个问题,有时过于简洁不是一件好事。你不能把你的if语句缩短。您需要Java确切地知道您正在检查什么是平等性。不过,正如在另一个答案中提到的,您也可以将月份放入一个集合中,但是如果要在输入字符串上调用s2.toLower(),请确保将它们全部拼写为小写