java 中的减法循环

Subtraction loops in java

我是 java 的新手,一直在接受这个挑战。我要根据输入的温度和高度来计算水的状态。然而,每 300 米(海拔高度)沸点将下降 1 度。我很困惑如何使它成为一个循环,每 300 次就取消一次,而不是在达到 300 次时才删除一次。这是我到目前为止所拥有的。 编辑:非常感谢您的帮助!甚至不知道人们是否再使用这个网站,但现在我会一直使用它 哈哈 :D

import java.util.Scanner;
public class WaterState
{
    public static void main(String[]args)
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter the temperature then the altitude separated by one or more spaces");
        double temperature = scan.nextDouble();
        double altitude = scan.nextDouble();
        double bp = 100;
        if (temperature <= 0)
        {
            System.out.println ("Water is solid at the given conditions");
        }
        if (temperature >= 1 && temperature < 100)
        {
            System.out.println ("Water is liquid at the given conditions");
        }
        if (temperature >= 100)
        {
            System.out.println ("Water is gas at the given conditions");
        }
    }
}

为什么你认为需要一个循环来计算沸点?想一想:给定一个高度,return 水的沸点。您实际上可以使用此信息计算熔点和沸点,然后只需检查您属于哪个范围。

import java.util.Scanner;
public class WaterState
{
    public static void main(String[]args)
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter the temperature then the altitude separated by one or more spaces");
        double temperature = scan.nextDouble();
        double altitude = scan.nextDouble();

        double offset = (int)altitude / 300.0;
        double boilingPoint = 100 - offset;
        double freezePoint = 0 - offset;
        if (temperature <= freezePoint)
        {
            System.out.println ("Water is solid at the given conditions");
        }
        if (temperature > freezePoint  && temperature < boilingPoint)
        {
            System.out.println ("Water is liquid at the given conditions");
        }
        if (temperature >= boilingPoint)
        {
            System.out.println ("Water is gas at the given conditions");
        }
    }
}