不可执行的源代码-表达式的非法开头



我在公共双温下收到一个错误。非常感谢您的帮助。我在谷歌上搜索了一下,仍然找不到代码中的错误。我相信这个问题应该很琐碎。

package temperature;
/**
*
* @author Logan
*/
public class Temperature
{
public static void main(String[] args)
{
// Use a floating-point number for the temperature and a character for the scale: either 'C' for Celsius or 'F' for Fahrenheit. T
public double temperature;
private char scale;
// Four constructors: one for the number of degrees, one for the scale, one for both the degrees and the scale, and a default constructor. For each of these constructors, assume zero degrees if no value is specified and Celsius if no scale is given.
public Temperature()
{
this(0.0, 'C');
}
public Temperature(double temp)
{
this(temp, 'C');
}
public Temperature(char scale)
{
this(0.0, scale);
}
public Temperature(double temp, char s)
{
temperature = temp;
scale = s;
}
// Two accessor methods: one to return the temperature in degrees Celsius,the other to return it in degrees Fahrenheit. Use the following formulas:
// Degrees_C = 5 (Degrees_F - 32) / 9
// Degrees_F = (9 (Degrees_C) / 5) + 32
// and round to the nearest tenth of a degree.
public double getDegreesF()
{
if(scale == 'F')
{
return round(temperature);
}
else
{
return round(9*temperature/5+32);
}
}
public double getDegreesC()
{
if(scale == 'C')
{
return round(temperature);
}
else
{
return round(5*(temperature-32)/9);
}
}
private double round(double input)
{
return (double)(((int)(input*10))/10.0);
}
// Three set methods: one to set the number of degrees, one to set the scale, and one to set both.
public void setDegrees(double temp)
{
temperature = temp;
}
public void setScale(char s)
{
scale = s;
}
public void setTemperature(double temp, char scale)
{
setDegrees(temp);
setScale(scale);
}
// Three comparison methods: one to test whether two temperatures are equal, one to test whether one temperature is greater than another, and one to test whether one temperature is less than another.
public boolean isEqualTo(Temperature rhs)
{
return rhs.getDegreesC() == getDegreesC();
}
public boolean isLessThan(Temperature rhs)
{
return rhs.getDegreesC() > getDegreesC();
}
public boolean isGreaterThan(Temperature rhs)
{
return rhs.getDegreesC() < getDegreesC();
}
}

我在公共温度加倍时出错了。非常感谢您的帮助。我在谷歌上搜索了一下,仍然找不到代码中的错误。

在java中的任何方法中,访问修饰符都是不合法的。在主方法之外定义类方法、变量和构造函数。代码应如下所示。

public class Temperature{
// Use a floating-point number for the temperature and a character for the scale: either 'C' for Celsius or 'F' for Fahrenheit. T
public double temperature;
private char scale;
// Four constructors: one for the number of degrees, one for the scale, one for both the degrees and the scale, and a default constructor. For each of these constructors, assume zero degrees if no value is specified and Celsius if no scale is given.
public Temperature()
{
this(0.0, 'C');
}
public Temperature(double temp)
{
this(temp, 'C');
}
public Temperature(char scale)
{
this(0.0, scale);
}
public Temperature(double temp, char s)
{
temperature = temp;
scale = s;
}
// Two accessor methods: one to return the temperature in degrees Celsius,the other to return it in degrees Fahrenheit. Use the following formulas:
// Degrees_C = 5 (Degrees_F - 32) / 9
// Degrees_F = (9 (Degrees_C) / 5) + 32
// and round to the nearest tenth of a degree.
public double getDegreesF()
{
if(scale == 'F')
{
return round(temperature);
}
else
{
return round(9*temperature/5+32);
}
}
public double getDegreesC()
{
if(scale == 'C')
{
return round(temperature);
}
else
{
return round(5*(temperature-32)/9);
}
}
private double round(double input)
{
return (double)(((int)(input*10))/10.0);
}
// Three set methods: one to set the number of degrees, one to set the scale, and one to set both.
public void setDegrees(double temp)
{
temperature = temp;
}
public void setScale(char s)
{
scale = s;
}
public void setTemperature(double temp, char scale)
{
setDegrees(temp);
setScale(scale);
}
// Three comparison methods: one to test whether two temperatures are equal, one to test whether one temperature is greater than another, and one to test whether one temperature is less than another.
public boolean isEqualTo(Temperature rhs)
{
return rhs.getDegreesC() == getDegreesC();
}
public boolean isLessThan(Temperature rhs)
{
return rhs.getDegreesC() > getDegreesC();
}
public boolean isGreaterThan(Temperature rhs)
{
return rhs.getDegreesC() < getDegreesC();
}
public static void main(String[] args)
{
//do coding here
}
}

最新更新