缺少正文,或声明抽象java错误消息

时间:2013-11-13 00:21:56

标签: java

我必须为作业制作一个面向对象的程序,我在第9行和第30行都得到了相同的错误。我知道我正在尝试创建Celsius和Fahrenheit对象错误,但我不知道如何做得正确。

import java.io.*;
class Celsius
{

        String inData;
        int celsius;
        int temperature;

    Celsius();
    {
     InputStreamReader inStream = new InputStreamReader (System.in);
         BufferedReader stdin = new BufferedReader (inStream);

     System.out.println("Enter a temperature in degres fahrenheit.");
     inData = stdin.readLine();
     temperature = Integer.parseInt(inData);

     celsius = (5 / 9) * (temperature - 32);
     System.out.println("Your temperature in degrees celsius is: " + celsius);
    }
}

class Fahrenheit
{

    String inData;
    int fahrenheit;
    int temperature;

    Fahrenheit();
    {
     InputStreamReader inStream = new InputStreamReader (System.in);
         BufferedReader stdin = new BufferedReader (inStream);

     System.out.println("Enter a temperature in degrees celsius.");
     inData = stdin.readLine();
     temperature = Integer.parseInt(inData);

      fahrenheit = (9 / 5) * temperature + 32;
      System.out.println("Your temperature in degrees fahrenheit is: " +  fahrenheit);
}
}

class TemperatureTest
{

public static void main(String[] args) throws IOException
{
InputStreamReader inStream = new InputStreamReader (System.in);
    BufferedReader stdin = new BufferedReader (inStream);
String inData;
int selection;

System.out.println("Input 1 to convert fahrenheit to celsius");
System.out.println("Input 2 to convert celsius to fahrenheit");


inData = stdin.readLine();
selection = Integer.parseInt(inData);

if (selection == 1)
{
 Celsius c1 = new Celsius();
}

if (selection == 2)
{
 Fahrenheit f1 = new Fahrenheit();
}

if (selection != 1 & selection != 2)
{
  System.out.println("Invalid selection.");
    }
    }
}

1 个答案:

答案 0 :(得分:2)

错误在你的构造函数上:

Celsius();
{

Fahrenheit();
{

构造函数/方法与其块之间不应有分号。删除那些分号:

Celsius()
{

Fahrenheit()
{

此外,在Java中,当两个int被划分时,会发生整数除法,这必须产生int。因此,(9 / 5)将产生1,而(5 / 9)将产生0

制作变量double,并为常量使用double字面值(或将其中一个作为double),以使用浮点除法:

(9.0 / 5.0)

( (double) 9 / 5)