0

割り当てのためにオブジェクト指向プログラムを作成する必要があります。9 行目と 30 行目の両方で同じエラーが発生します。摂氏と華氏のオブジェクトを間違って作成しようとしていることはわかっていますが、その方法がわかりません。正しく。

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.");
    }
    }
}
4

1 に答える 1

2

エラーはコンストラクタにあります:

Celsius();
{

Fahrenheit();
{

コンストラクター/メソッドとそのブロックの間にセミコロンがあってはなりません。これらのセミコロンを削除します。

Celsius()
{

Fahrenheit()
{

さらに、Java では、2 つintの が除算されるときに整数除算が発生し、int. その結果、(9 / 5)が得られ1(5 / 9)が得られ0ます。

変数を作成し、定数doubleにリテラルを使用double(または定数の 1 つを としてキャストdouble) して、浮動小数点除算を使用します。

(9.0 / 5.0)

また

( (double) 9 / 5)
于 2013-11-13T00:23:32.217 に答える