Java中温度程序的类声明

时间:2013-12-04 21:29:20

标签: java class methods declaration temperature

所以这就是我需要输入这五种方法的问题:

  • 温度 - 构造方法。这应该将初始温度设置为100。
  • getTemp - 返回当前存储的摄氏度值
  • 的方法
  • convertToF - 将摄氏温度转换为华氏度的方法
  • convertToK - 将摄氏温度转换为开尔文度的方法
  • updateTempC - 以摄氏度更新存储温度的方法

进入这个计划:

import java.util.Scanner;

public class TempProg {



public static void main(String[] args)
{

    // Declare objects
    Scanner scan = new Scanner(System.in);
    Temperature tempConv = new Temperature();

    // Declare variables
    int newTemp;
    boolean entryValid;

    // Declare constants
    final int MIN_TEMP = -273;
    final int MAX_TEMP = 10000;

    System.out.println("\tTemperature converter");

    // Set a dummy selection value, so that we always show the options on the first go
    char selection = 'x';

    // Offer a list of options
    while (selection != 'q') { 
        System.out.println("\n\tCurrent temperature in degrees C: " + tempConv.getTemp());
        System.out.println("\tType f to display temperature in Fahrenheit");
        System.out.println("\tType k to display temperature in Kelvin");
        System.out.println("\tType c to set a new temperature");
        System.out.println("\tType q to quit");

        // Read from the keyboard
        selection = scan.next().charAt(0);


        // Act on the selection
        switch(selection) {
            case 'f': // Print Fahrenheit version
                System.out.println("\n\t" +tempConv.getTemp()+ " degrees C = "+tempConv.convertToF() +" degrees F" );
                break;

            case 'k': // Print Kelvin version 
                System.out.println("\n\t" +tempConv.getTemp()+ " degrees C = "+tempConv.convertToK() +" degrees K" );
                break;

            case 'c': // Get a new temperature

                entryValid=false; // Reset entryValid for this round

                /* test for !entryValid
                 * i.e. "not entryValid"
                 * i.e. same as "entryValid == false"
                 */

                while (!entryValid) { // This will always be true the first time
                    System.out.print("\n\tPlease enter a new temperature: ");
                    newTemp = scan.nextInt();

                    // Check validity of new temperature
                    if (newTemp < MIN_TEMP || newTemp > MAX_TEMP) {
                        System.out.println("\tPlease enter a valid temperature");
                    } else {
                        entryValid=true;
                        tempConv.updateTempC(newTemp);
                    }
                }

                break;

            case 'q': // Don't do anything for q, we print a message later
                break;

            default: // If it is not f, k, c or q then default is error message
                System.out.println("\n\tOption " + selection + " not understood");
        }
    }
    System.out.println("\n\tPROGRAM ENDED");
}
}

这就是我到目前为止......

private double Temperature (double currentTemp)
    {
        currentTemp = 100;
        return currentTemp;

    }

    public double convertToF (double TempF)
    {
        TempF = ((9 * currentTemp) / 5 ) + 32;
        return TempF();
    }

    public double convertToK (double TempK)
    {
        TempK = currentTemp + 273;
        return TempK();
    }

    public void updateTempC (double currentTemp)
    {
        newTemp = currentTemp;

        return currentTemp();
    }

    public double getTemp()
    {
        return currentTemp;
     }

它基本上不会编译,我99%肯定这是非常错的,我真的不知道该做什么...想法...建议?

并且错误是:

    tempProg.java:14: error: class TempProg is public, should be declared in a file named            TempProg.java
public class TempProg {
       ^
    tempProg.java:26: error: cannot find symbol
            TempF = ((9 * currentTemp) / 5 ) + 32;
                          ^
  symbol:   variable currentTemp
  location: class TempProg
    tempProg.java:27: error: cannot find symbol
            return TempF();
                   ^
  symbol:   method TempF()
  location: class TempProg
    tempProg.java:32: error: cannot find symbol
            TempK = currentTemp + 273;
                    ^
  symbol:   variable currentTemp
  location: class TempProg
    tempProg.java:33: error: cannot find symbol
            return TempK();
                   ^
  symbol:   method TempK()
  location: class TempProg
    tempProg.java:38: error: cannot find symbol
            newTemp = currentTemp;
            ^
  symbol:   variable newTemp
  location: class TempProg
    tempProg.java:40: error: cannot return a value from method whose result type is void
            return currentTemp();
                              ^
    tempProg.java:45: error: cannot find symbol
            return currentTemp;
                   ^
  symbol:   variable currentTemp
  location: class TempProg
    tempProg.java:54: error: cannot find symbol
        Temperature tempConv = new Temperature();
        ^
  symbol:   class Temperature
  location: class TempProg
    tempProg.java:54: error: cannot find symbol
        Temperature tempConv = new Temperature();
                                   ^
  symbol:   class Temperature
  location: class TempProg

2 个答案:

答案 0 :(得分:2)

您需要Temperature课程。考虑到不正确的“构造函数”,我只是猜测你已经将这些方法添加到TempProg。你需要的是另一个类,如此......

public class Temperature {
  // Put those method and data members here
}

构造函数不应返回值。它应该看起来像...... public Temperature(),或者根据您的具体要求而变化。

答案 1 :(得分:0)

<强>输出 output

  

默认温度:0.0C或32.0F

     

1.将Celcius转变为Fareiheit

     

2.转换到Celcius的Fareiheit

     

3.更新默认温度

     

1

     

在Celcius中输入温度以转换为Farenheit

     

60

     

60.0C = 92.0F

     

默认温度:0.0C或32.0F

     

1.将Celcius转变为Fareiheit

     

2.转换到Celcius的Fareiheit

     

3.更新默认温度

     

2

     

以Farenheit输入温度以转换为Celcius

     

-10

     

-10.0F = -23.333333333333336C

     

默认温度:0.0C或32.0F

     

1.将Celcius转变为Fareiheit

     

2.转换到Celcius的Fareiheit

     

3.更新默认温度

     

3

     

以celcius输入温度

     

25

     

默认温度:25.0C或57.0F

     

1.将Celcius转变为Fareiheit

     

2.转换到Celcius的Fareiheit

     

3.更新默认温度

<强> Temperature.java

public interface Temperature {
    public double getTempInFarenheit(double celcius);
    public double getTempInCelcius(double farenheit);
    public double getCurrentTemp();
    public double setDefaultTemp(double defaultCelcius);
    }

<强> TemperatureImpl.java

public class TemperatureImpl implements Temperature {

    private double defaultTemp=0.0;

    public double Temperature(double defaultTemp){
        return this.defaultTemp=defaultTemp;
    }

    @Override
    public double getTempInFarenheit(double celcius) {
        return ((double)(9/5)*(celcius+32.0));
    }

    @Override
    public double getTempInCelcius(double farenheit) {
        return ((double)5/9*(farenheit-32.0));
    }

    @Override
    public double getCurrentTemp() {
        return defaultTemp;
    }

    @Override
    public double setDefaultTemp(double defaultCelcius){
        return this.defaultTemp = defaultCelcius;
    }
}

<强> Main.java

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        TemperatureImpl temp = new TemperatureImpl();

        while(true){
            System.out.println("Default temperatures: "+temp.getCurrentTemp()+"C OR "+temp.getTempInFarenheit(temp.getCurrentTemp())+"F");
            System.out.println("1.Convert Celcius to Fareiheit");
            System.out.println("2.Convert Fareiheit to Celcius");
            System.out.println("3.Update default temperature");
            Scanner sc = new Scanner(System.in);
            int input = sc.nextInt();
            switch (input) {
            case 1: System.out.println("Enter temperature in Celcius to convert into Farenheit");
                double celcius = sc.nextDouble();
                System.out.println(celcius+"C = "+temp.getTempInFarenheit(celcius)+"F");
                break;
            case 2: System.out.println("Enter temperature in Farenheit to convert into Celcius");
            double fareinheit = sc.nextDouble();
            System.out.println(fareinheit+"F = "+temp.getTempInCelcius(fareinheit)+"C");
            break;
            case 3: System.out.println("Enter temperature in celcius");
            temp.setDefaultTemp(sc.nextDouble());
                break;
            default:
                System.out.println("Invalid input.");;
            }

        }
    }
}