我希望输入不区分大小写

时间:2015-06-23 15:19:38

标签: java

例如,当用户键入“九月”,“九月”,“九月”,“九月”或无论如何只要字母正确就是有效条目。什么是编写此代码的更简洁的方法(我使用“||”的部分。)?

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();
          }

     }

}

7 个答案:

答案 0 :(得分:6)

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

答案 1 :(得分:3)

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

答案 2 :(得分:3)

对于不区分大小写的情况,您应该查看使用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;
    }
}

答案 3 :(得分:1)

// 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 :(得分:1)

如何将所有月份名称添加到列表(全部小写)并调用list.contains(input.toLowerCase())?这将使你的" ||"部分更具可读性。

答案 5 :(得分:1)

虽然我的回答也使用了其他人建议的类似方法,但我不建议大量ORif条件。所以我建议下面的解决方案,我相信可以增加更多的价值:

package com.test;

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

public class Compare {

    public static void main(String[] args) {

        String s1 = getInput("Enter a month ");
        Month month = Month.valueOf(s1.toUpperCase());
        if (month.isFebruary()) {
            System.out.println("It's time to go to the Disneyland !");
            String s2 = getInput("Enter another month: ");
            month = Month.valueOf(s2.toUpperCase());
            if (month.isFebruary()) {
                System.out
                        .println("You already won a Disneyland ticket! Try       another month.");
                String s3 = getInput("Enter another month: ");

                month = Month.valueOf(s3.toUpperCase());
                if (!month.isFebruary()) {
                    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();
        }

    }

}

enum Month {
    JANUARY, 
    FEBRUARY,
    MARCH,
    APRIL,
    MAY,
    JUNE, 
    JULY,
    AUGUST, 
    SEPTEMBER,
    OCTOBER,
    NOVEMBER,
    DECEMBER;

    public boolean isFebruary() {
        return this == FEBRUARY;
    }
}

答案 6 :(得分:1)

您也可以考虑将所有月份放入数组并进行更简单的比较:

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

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