我是 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");
}
}
}
uj5u.com熱心網友回復:
為什么你認為需要一個回圈來計算沸點?想一想:給定一個高度,回傳水的沸點。您實際上可以使用此資訊計算熔點和沸點,然后只需檢查您落在哪個范圍內。
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");
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/336415.html
上一篇:Java問題中的While回圈
下一篇:ansible和sub回圈
