Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.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_Temperature - Fatal编程技术网

Java 温度转换器

Java 温度转换器,java,temperature,Java,Temperature,我有一个温度转换器的任务。代码已经提供了,我必须添加一些东西来让它工作。我的问题是,当scales是字符串数组时,如何为scale返回char 任何有**代码**的东西都是我写的 public class Temperature{ **private double temp; private char scale;** /** different scale names */ public static String[] scales = {"Celsius", "Fahrenheit", "K

我有一个温度转换器的任务。代码已经提供了,我必须添加一些东西来让它工作。我的问题是,当scales是字符串数组时,如何为scale返回char

任何有**代码**的东西都是我写的

public class Temperature{
**private double temp;
private char scale;**
/** different scale names */
public static String[] scales = {"Celsius", "Fahrenheit", "Kelvin"};
**scales[0] = "C";
scales[1] = "F";
scales[2] = "K";**

public Temperature(){
  **temp = 0.0;
  scale = scales[0];**
}



 /** Initializes a temperature object with given value in Celcius
  *  
  *  If the initial temperature is less than -273.15 then the temperature 
  *  object will be initialized with -273.15C.
   *   
  * @param temp is the initial temperature in Celsius.
  */
public Temperature(double temp){
  **this.temp = temp;
  if(temp < (-273.15)){
    temp = (-273.15);
  }
  scale = scales[0];**
}


 /** Initializes a temperature object with given value using the specified scale
 * <par>
* If the temperature is lower than absolute zero, then set the temperature to
 * absolute zero (in whichever scale is specified).
 * <par>  
 * Examples: new Temperature(12.3, "K")
 *           new Temperature(-90.2, "Celsius")
*
* @param temp is the initial temperature
* @param scale is the scale of initial temperature and must either be 
*        one of the Strings in the <code>scales</code> array, or 
*        the first letter (capitalized) of one of those strings.
*/
public Temperature(double temp, String scale){
  **this.temp = temp;
  if(temp < (-273.15)){
    temp = (-273.15);
  }
  scale = scales[];**
}



/** The output of this getter method must always be the first letter of one
  * of the strings in the <code>scales</code> array, capitalized.
   *  
  * @return the current scale of the object as a single char (the first letter, 
  *         capitalized of one of the strings from <code>scales</code>)
  */
  public char getScale(){
    return 'X';
  }

根据您的问题,您的最后一个构造函数应该如下所示

public Temperature(double temp,String scale){
    if(scale.equals("C"))
    {
            if(temp<-273.15)
            {
                this.temp=-273.15;
            }
            else
                this.temp=temp;
        }
        else if(scale.equals("F"))
        {
            if(temp<-459.67)
            {
                this.temp=-459.67)
            }
            else
                this.temp=temp;
        }
        else if(scale.equals("K"))
        {
            if(temp<0)
            {
                this.temp=0;
            }
            else
                this.temp=temp;
        }
        this.scale=scale;
    }
公共温度(双温、串级){
if(比例等于(“C”))
{

如果(你可以运行它来看看它是否有意义。@Henry how?没有
main
@DawoodibnKareem不太难添加一个用于测试的,不是吗?@s.Okita你可能需要比这个网站上的人能给你的更多的帮助。我强烈推荐1.你没有做任何转换。2.你的问题是什么?
If(刻度==“C”)
:认真?为什么只在温度超出范围时才设置温度?很抱歉输入错误@Henry@ShivKumar这不是关于错字。你确定你想用<代码> = = /COD>比较<代码>字符串< /代码>?这是因为我在C++中的习惯。