【问题标题】:The operator < is undefined for the argument type(s) LocalTime, int in java运算符 < 未定义参数类型 LocalTime,java 中的 int
【发布时间】:2022-12-13 07:51:02
【问题描述】:

我这里有这段代码:

但我收到错误消息:

运算符 < 未定义参数类型 LocalTime, int

这是为什么?我该如何修复代码?

这是再次作为文本的代码:

import java.time.LocalTime;

public class Services {
    public static void main(String[] args){
        LocalTime t = LocalTime.now();
        
        if (t >=0 && t<12){
            System.out.println("Good Morning!");
        }
        else if (t>=12 && t<18)
        {
            System.out.println("Good Afternoon!");   
        }
        else{
            System.out.println("Hello Neel ,how may I help you");  

               
        }
    }
}

【问题讨论】:

    标签: java datetime


    【解决方案1】:

    比较对象

    除了像 int 这样的基元,你不能在其他任何东西上使用 &lt;。请改用compareTo

    比如first.compareTo(second),结果要么

    • 负(如果更小),
    • 0(如果等于)或
    • 正大于 0(如果大于)。

    所以等同于first &lt; second 将是first.compareTo(second) &lt; 0


    特别比较LocalTime

    为了java.timeAPI,还有像isBeforeisAfter这样的特殊方法,使这种比较更加简单。


    LocalTimeint

    此外,您不能将诸如 LocalTime 之类的高级对象与普通的 int 进行比较。看一下

    LocalTime.of(12, 0)
    

    和类似的方法。

    还有一些特殊的预创建常量,例如LocalTime.MIDNIGHTLocalTime.NOON


    把一切放在一起

    如果您遵循这两个建议,固定代码可能如下所示:

    LocalTime t = LocalTime.now();
    
    if (t.isAfter(LocalTime.MIDNIGHT) && t.isBefore(LocalTime.NOON)) {
      System.out.println("Good Morning!");
    } else if (t.isAfter(LocalTime.NOON) && t.isBefore(LocalTime.of(18, 0))) {
      System.out.println("Good Afternoon!");   
    } else {
      System.out.println("Hello Neel, how may I help you"); 
    }
    

    理想情况下,您还可以引入一种快速帮助方法,例如

    private static boolean isBetween(LocalTime start, LocalTime time, LocalTime end) {
      return time.isAfter(start) && time.isBefore(end);
    }
    

    进一步简化代码:

    LocalTime t = LocalTime.now();
    
    if (isBetween(LocalTime.MIDNIGHT, t, LocalTime.NOON)) {
      System.out.println("Good Morning!");
    } else if (isBetween(LocalTime.NOON, t, LocalTime.of(18, 0))) {
      System.out.println("Good Afternoon!");   
    } else {
      System.out.println("Hello Neel, how may I help you"); 
    }
    

    【讨论】:

      【解决方案2】:

      将您的代码翻译成可用的东西的直接方法是

       LocalTime t = LocalTime.now();
       int h = t.getHour();
      

      然后比较'h'。

      对于更复杂的情况,例如,如果你想检查它是否在 12:30 之前,请查看像这样的结构

       t.isBefore(LocalTime.of(12, 30))
      

      或者可能

       !LocalTime.of(12, 30).isAfter(t);
      

      (两者在准确时间12:30的决定上有所不同)

      【讨论】:

      • 本地时间 t = LocalTime.now(); int h = t.getHour();是的,这个对我有用,谢谢
      【解决方案3】:

      好吧,这非常有用!我自己是初学者,一直在玩 if 语句。在答案的帮助下,我设法拼凑了一些简单的东西:

      import java.time.LocalTime;
      
      public class Main {
      
          public static void main(String[] args) {
      
              LocalTime currentTime = LocalTime.now();
      
              System.out.println("The current time is "+currentTime);
      
              int hour = currentTime.getHour();
              int timeOfDay = 10;
      
              if (hour >= 0 && hour < 12) {
                  System.out.print("Good Morning!");
              } else if (hour >= 12 && hour < 18) {
                  System.out.print("Good Afternoon!");
              } else {
                  System.out.print("Good Evening!");
              }
          }
      }
      

      因为“isBetween”在主类中不起作用

      【讨论】:

        【解决方案4】:
            public static void main(String[] args) {
            
            LocalTime t = LocalTime.now();
            int h = t.getHour();
            if (h >= 0 && h < 12) {
                System.out.println("Good Morning!");
            } else if (h >= 12 && h < 20) {
                System.out.println("Good Afternoon!");
            } else {
                System.out.println("Hello Neel ,how may I help you");
        
            }
        }
        

        }这对我有用

        【讨论】:

        • 正如目前所写,您的答案尚不清楚。请edit 添加更多详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写出好的答案的信息in the help center
        • 欢迎来到 Stack Overflow,感谢您的贡献。虽然您的代码是正确的,但您的答案可能需要一些解释。甚至你们都不想回答这是为什么?问题的一部分,请仍然解释你的代码是如何工作的。
        猜你喜欢
        • 2017-06-04
        • 1970-01-01
        • 2018-06-30
        • 2018-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-10
        相关资源
        最近更新 更多