使用if语句计算水的物质状态?

时间:2014-07-01 01:58:05

标签: java

我需要通过用户输入摄氏温度或华氏温度和高度(英尺或米)来计算固体,液体或气体的状态。

当我尝试运行程序时,它总是说水是气态的。我似乎无法弄清楚我错过了什么。或做错了。

public class Water 
{

   private final double temperature;
   private final String degrees;
   private final int altitude;
   private final String units;
   private final double bpFeet;
   private final double bpMeters;
   private String waterState;


   public Water(double temp, String deg, int alt, String unitType)
   {
       temperature = temp;
       degrees = deg;
       altitude = alt;
       units = unitType;
       bpFeet = temperature - (altitude * 0.001); //boiling point given an altitude in feet
       bpMeters = temperature - (altitude * 0.01); //boiling point given an altitude in meters
   }

   public String getState()
   {

        if (degrees.equals("F"))
        {
                   if (units.equals("Ft"))
                   {
                       if (temperature >= bpFeet)
                       {
                           waterState = "The water is gaseous.";
                           System.out.println(waterState);
                       }
                       else if (temperature > 32 && temperature < bpFeet)
                       {
                           waterState = "The water is liquid.";
                           System.out.println(waterState);
                       }
                       else
                       {
                           waterState = "The water is solid.";
                           System.out.println(waterState);
                       }
                   }

                   else if (units.equals("M"))
                   {
                       if (temperature <= bpMeters)
                       {
                           waterState = "The water is solid.";
                           System.out.println(waterState);
                       }
                       else if (temperature > 32 && temperature < bpMeters)
                       {
                           waterState = "The water is liquid.";
                           System.out.println(waterState);
                       }
                       else 
                       {
                           waterState = "The water is solid.";
                           System.out.println(waterState);
                       }
                   }
                   else
                   {
                       System.out.println("Error: must enter FT (feet) or M (meters)");
                   }
        }

        else if (degrees.equals("C"))
        {
                   if (units.equals("Ft"))
                   {
                       if (temperature >= bpFeet)
                       {
                           waterState = "The water is gaseous.";
                           System.out.println(waterState);
                       }
                       else if (temperature > 0 && temperature < bpFeet)
                       {
                           waterState = "The water is liquid.";
                           System.out.println(waterState);
                       }
                       else
                       {
                           waterState = "The water is solid.";
                           System.out.println(waterState);
                       }
                   }

                   else if (units.equals("M"))
                   {
                       if (temperature <= bpMeters)
                       {
                           waterState = "The water is solid.";
                           System.out.println(waterState);
                       }
                       else if (temperature > 0 && temperature < bpMeters)
                       {
                           waterState = "The water is liquid.";
                           System.out.println(waterState);
                       }
                       else 
                       {
                           waterState = "The water is solid.";
                           System.out.println(waterState);
                       }
                   }
                   else
                   {
                       System.out.println("Error: must enter FT (feet) or M (meters)");
                   }
       }

       else
       {
           System.out.println("Error: must enter C (centigrade) or F (farenheit)");
       }



           return temperature + degrees + " " + altitude + units;


   }


}

这是我用来测试这个类的主要方法的类。

public class WaterState 
{
    public static void main(String[] args)
    {
    Water timWater = new Water(-100, "C", 1000, "Ft");


    String newWater = timWater.getState();
    System.out.println(newWater);

    }
}

1 个答案:

答案 0 :(得分:1)

问题区域是以下代码:

您应该检查计算。

bpFeet = temperature - (altitude * 0.001); // boiling point given an altitude in feet
        bpMeters = temperature - (altitude * 0.01); // boiling point given an altitude in meters

无论你放入什么温度,bpFeet总会更少,因此当你进行比较if (temperature >= bpFeet)时,它总是会返回true,从而使水成为气态。