无法编译的源代码-表达式的非法开头

时间:2018-11-05 10:06:27

标签: java

在公共双重温度下出现错误。任何帮助是极大的赞赏。我用谷歌搜索,但仍然找不到我的代码中的错误。我确定问题应该很简单。

   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();
}
}

在公共双重温度下出现错误。任何帮助是极大的赞赏。我用谷歌搜索,但仍然找不到我的代码中的错误。

1 个答案:

答案 0 :(得分:0)

访问修饰符在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
        }
    }