【问题标题】:I'm writing code to determine the state of water with if statements我正在编写代码来使用 if 语句确定水的状态
【发布时间】:2014-11-13 02:41:19
【问题描述】:

我正在编写代码来根据摄氏度和海拔高度来确定水的状态,最后我该如何做到这一点,如果海拔超过 300,则水需要少 1 摄氏度煮沸并根据输入确定结果?

import java.util.Scanner;

public class WaterState
{       
    public static void main(String[] args)
    {
        boolean run = true;
        while(run)
        {
            Scanner scan = new Scanner(System.in);

            System.out.println("What is the temperature in degrees Celsius?");
            double temp = scan.nextDouble();
            System.out.println("What is the height above Sea level? in exact meters");
            double altitude = scan.nextDouble(); 

            int freezingPoint = 0;
            double alternateAltitude = temp - (altitude * 0.001);
            double boilingPoint = 100;
            int baseAltitude = 300;

            if ((temp <= freezingPoint) && (altitude <= baseAltitude))
            {
                System.out.println("Water is frozen");
            }
            else if ((temp > freezingPoint) && (altitude <= baseAltitude))
            {
                System.out.println("Water is liquid");
            }
            else if ((temp >= boilingPoint) && (altitude <= baseAltitude))
            {
                System.out.println("Water is gaseus");
            }

            else if ((altitudeChange > )
            {  

                System.out.println("Water is boiling, but at a lower temperature because of the altitude");

        }
    }
}
}

【问题讨论】:

  • 我不确定我是否得到了确切的问题,但我猜你正在寻找类似if (altitude &gt; 300 &amp;&amp; temp &gt;= boilingPoint - 1)
  • 如果高度为> baseAltitude,则只需更改沸点。
  • @HovercraftFullOfEels 虽然确实没有人根据贴有“紧急”标签的问题来回答问题,但我认为这对任何人都不是真正的不公平。它只是空虚。
  • @OlaviMustanoja:不管怎样,1)我提供信息以帮助他们解决未来的问题,2)我给了他答案,3)否决票不是我的。
  • @HovercraftFullOfEels 我没有对您的第 1、2 或 3 点发表任何评论

标签: java if-statement logic java.util.scanner temperature


【解决方案1】:

以下是与您的问题相关的内容:

        else if ((altitudeChange > ){ 
            System.out.println("Water is boiling, but at a lower temperature because of the altitude");
        }

根据您的输入:

如果海拔超过 300 度,则需要少 1 摄氏度 煮沸的水和结果可以取决于 输入

这是你想要的吗:

if(altitude >=baseAltitude  && temp>(boilingPoint -(altitude/baseAltitude *1)) ){//1.altitude is more than 300,2.temp is higher than the new degree celsius to boil 
 System.out.println("Water is boiling, but at a lower temperature because of the altitude");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-03
    • 1970-01-01
    • 2012-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多