Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 检查温度条件_Java - Fatal编程技术网

Java 检查温度条件

Java 检查温度条件,java,Java,我如何在这三种条件下设置适当的温度(至少98℃、99℃、1℃和2℃等) package boiling.freezing; import java.util.Scanner; public class BoilingFreezing { public static void main(String[] args) { System.out.println("Give the temperature : "); Scanner sc = new Sca

我如何在这三种条件下设置适当的温度(至少98℃、99℃、1℃和2℃等)

package boiling.freezing;

import java.util.Scanner;

public class BoilingFreezing {

    public static void main(String[] args) {
        System.out.println("Give the temperature : ");
        Scanner sc = new Scanner(System.in);
        int temp = sc.nextInt();
        if (temp >= 100){
            System.out.println("The water is boiling!");
        }
        else if (temp <= 0){
            System.out.println("The water is freezing!");
        }
        else{
            System.out.println("The water is at normal_state!");
        }
    } 
}
我要给你一个模式,而不是简单的if-elseif-else块

您可能需要一个接口温度

然后可以实现此接口以满足多个条件

class BoilingTemperature implements Temperature {
   public boolean within(final int temperature) {
      return temperature > 99;
   }

   public String message() {
      return "Water boiling";
   }
}

class FreezingTemperature implements Temperature {
   public boolean within(final int temperature) {
      return temperature < 1; // Should be 3 degree! But anyway.
   }

   public String message() {
      return "Water freezing";
   }
}

根据LppEdd的回答,如果您使用的是java8+,那么可以使用Java谓词

引入了java.util.function.Predicate,它在lambda表达式和 功能接口。谓词的功能方法是 testObject


我想创建这样一个程序,在这个程序中可以检查适当的温度,即使是98℃、99℃、1℃、2℃等等。我的程序的上面部分并没有很好地解决这个问题。。。它在98°C和99°C时显示正常状态,而不是我希望它显示的状态:接近沸腾@ayeshariaz_ash查看我的答案。请编辑问题以解释你想做什么以及你的问题是什么@ayeshariaz_ash我不太了解,但我会尝试一下!LppEdd您认为使用谓词将更干净地实现这一点吗?不管怎样,我对你的答案投了赞成票。@sandeshdahake我想说,拥有一个描述你正在工作的领域的界面总是更好的。在这个特殊的例子中,我把它写成温度,但其他词可能更合适。谓词是可以的,但它更一般。例如,我在处理流时会使用谓词,因为操作包含在一小段代码中。谓词可以传递给lambda,但不一定总是流。我仍然更喜欢使用函数接口而不是编写own@sandeshdahake温度接口可视为功能接口。使用自定义接口对代码建模通常比使用预定义的、全面的接口要好。但如果您觉得更习惯于使用JDK,那么就不要这么做。这根本不是问题。重要的是代码必须清晰。这就是全部;
class BoilingTemperature implements Temperature {
   public boolean within(final int temperature) {
      return temperature > 99;
   }

   public String message() {
      return "Water boiling";
   }
}

class FreezingTemperature implements Temperature {
   public boolean within(final int temperature) {
      return temperature < 1; // Should be 3 degree! But anyway.
   }

   public String message() {
      return "Water freezing";
   }
}
class YourCustomTemperature implements Temperature {
   public boolean within(final int temperature) {
      return temperature > 6 && temperature < 40;
   }

   public String message() {
      return "Your custom message";
   }
}
final List<Temperature> temperatures = new ArrayList<>(6);
temperatures.add(new BoilingTemperature());
temperatures.add(new FreezingTemperature());
temperatures.add(new YourCustomTemperature());
...
public static void main(String[] args) {
    System.out.println("Give the temperature : ");
    final Scanner sc = new Scanner(System.in);
    int temp = sc.nextInt();

    for (final Temperature t : temperatures) {
       if (t.within(temp)) {
          System.out.println(t.message());
       } 
    }
}