【问题标题】:Can I create an OR statement when comparing strings?比较字符串时可以创建 OR 语句吗?
【发布时间】:2013-05-10 17:20:35
【问题描述】:

我的代码是:

import java.util.Scanner;

class mainClass {
    public static void main (String [] args) {          
        secondaryClass SCO = new secondaryClass();          
        Scanner scanner = new Scanner(System.in);           
        String randomtext = scanner.nextLine();    
        if(randomtext.equals("What is the time"))
        {
            SCO.giveTime();               
        }
        else if (randomtext.equals("Whats the time"))
        {       
            SCO.giveTime();             
        }
    }       
}

我想知道是否可以将 if else 语句替换为以下内容:

import java.util.Scanner;

class mainClass {
    public static void main (String [] args) {  
        secondaryClass SCO = new secondaryClass();      
        Scanner scanner = new Scanner(System.in);       
        String randomtext = scanner.nextLine();
        if(randomtext.equals("What is the time" || "Whats the time"))
        {
            SCO.giveTime();
        }       
    }
}

顺便说一句,SCO是我第二课的对象,它完美地输出了时间。

【问题讨论】:

    标签: java string if-statement


    【解决方案1】:

    您可以使用正则表达式进行比较,但它只会将 OR 从 java 移动到正则表达式:

    if (randomtext.matches("(What is the time)|(Whats the time)"))
    

    虽然你可以更简洁地表达:

    if (randomtext.matches("What(s| is) the time"))
    

    甚至可以选择撇号和/或问号:

    if (randomtext.matches("What('?s| is) the time\\??"))
    

    【讨论】:

      【解决方案2】:

      你需要这样写:

      if (randomtext.equals("What is the time") || randomtext.equals("Whats the time"))
      

      【讨论】:

      • 这绝对有效,但你“不需要”这样说。
      • 显然还有其他方法可以做到这一点,但这最接近 OP 试图表达的内容。
      【解决方案3】:

      您使用|| 逻辑或运算符是正确的,但您使用它的方式是错误的。从您的第一个示例中获取ifelse if 中的每个特定条件,并将|| 放在它们之间的一个if 中,不需要else if

      【讨论】:

        【解决方案4】:

        最明显的方法是这样做:

        if(randomtext.equals("What is the time") || randomtext.equals("Whats the time"))
        {
              SCO.giveTime();
        }
        

        但从 JDK 7 开始,您可以使用 switch 语句:

        switch (randomtext) {
            case "What is the time":
            case "Whats the time":
                SCO.giveTime();
                break;
        }
        

        【讨论】:

          【解决方案5】:

          换个角度:

          import java.util.Scanner;
          
          class mainClass {
              public static void main (String [] args) {
                  secondaryClass SCO = new secondaryClass();
          
                  Scanner scanner = new Scanner(System.in);
          
                  String randomtext = scanner.nextLine();
          
                  List<String> stringsToCheck = new ArrayList<String>();
                  stringsToCheck.add("What is the time");
                  stringsToCheck.add("Whats the time");
          
                  if (stringsToCheck.contains(randomtext)) {
                        SCO.giveTime();
                  }       
              }
          }   
          

          【讨论】:

            【解决方案6】:

            假设你在一个条件语句中只有两个可能的选项,你可以使用这个

            randomtext = Condition1 ? doesThis() : doesThat();
            

            附言我不会做“案例”。在这种情况下,它并不重要,因为它只有两个选项,但是当您使用案例时,每个案例行都将根据条件“TRUE”单独检查,这对于长程序可能需要很长时间。

            【讨论】:

            • 我对你投了反对票,因为你误解了这个问题——在这种情况下,条件语句没有帮助。 IIRC 我在 Java 101 中获得了 A+ :)
            • 我是 LOTUS Marketing Solutions 的一名高级 Java 程序员。我告诉你它确实有效。但我没有时间做这个。所以继续认为你是对的并误导其他人。这个网站不是关于谁知道和谁帮助。这个网站充满了不成熟和有针对性的投票。 :等待暂停:
            • 条件语句有效,但在这种情况下它们没有帮助。你的阅读理解如何?此外,“case”语句是fast。停止抱怨,你不会被“暂停”。在我看来,你听起来不像是高级开发人员。
            • 如果您愿意,欢迎您来为我们工作。而且,为时已晚......刚刚被停职......这是没有高中毕业经验的版主所做的;)
            猜你喜欢
            • 2016-07-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-03-17
            • 2013-05-06
            相关资源
            最近更新 更多