当前位置: 首页 > 知识库问答 >
问题:

Java中温度程序的类声明

咸星波
2023-03-14

这就是我遇到的问题,我需要输入以下五种方法:

    null
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;
     }

和错误是:

    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

共有1个答案

柳灿
2023-03-14

您需要一个temperate类。我只是猜测您已经将这些方法添加到tempprog中,考虑到不正确的“构造函数”。你需要的是另一门课,像这样...

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

构造函数不应返回值。看起来应该像...公共温度(),或根据您的具体要求进行某些变化。

 类似资料:
  • 本文向大家介绍C#程序将摄氏温度转换为华氏温度,包括了C#程序将摄氏温度转换为华氏温度的使用技巧和注意事项,需要的朋友参考一下 首先,设置摄氏温度- 现在将其转换为华氏度: 您可以尝试运行以下代码将摄氏温度转换为华氏温度。 示例 输出结果

  • 嗨,我正在尝试创建一个gui,它将在每次温度传感器发回信号并更新jtag值时更新温度。现在我可以通过按下更新按钮来更新gui标签,但是我希望它在不按下“更新按钮”的情况下自动更新。我尝试了很多方法,包括和,并使用不同类型的摆动计时器,但仍然无法让它工作。有人能在这里帮我吗?谢谢我会在底部发布我的代码。 GUI类: 串行读卡器类: 这是我的gui的图像, http://i692.photobucke

  • 我对Java还是个新手,在理解OOP方面更是新手,所以请不要取笑我的不理解。 我试图设计一个程序,让用户输入华氏或摄氏度的温度,然后程序将确定另一个测量中的温度。 有没有人能给我一些建议,告诉我我的方向是否正确? 这是我到目前为止所做的,请记住,这是我对OOP的第一次尝试,所以它可能看起来一团糟。

  • Python3 实例 以下实例演示了如何将摄氏温度转华氏温度:# -*- coding: UTF-8 -*- # Filename : test.py # author by : www.runoob.com # 用户输入摄氏温度 # 接收用户输入 celsius = float(input('输入摄氏温度: ')) # 计算华氏温度 fahrenheit = (celsius * 1.8) +

  • 我试图创建一个类,它有一个接受温度(以摄氏度为单位)为双值的构造函数,如果温度小于-273.15,则将其设置为-273.15。它还可以计算不同测量单位的其他温度,但这并不重要。由于某种原因,我得到一个逻辑错误,它不能纠正小于-273.15到-273.15的输入。

  • 我正在做一个项目,需要我使用TinkerKit热敏电阻传感器来获得室温。检测后,Arduino BT板必须通过蓝牙将串行数据发送回Android手机。温度需要在Celcius的手机屏幕上显示。 我已经使串行通信链接工作。但热敏电阻的读数似乎很奇怪。它的读数是173/174/175,但我不确定它的读数是华氏度还是摄氏度(因为我找不到任何数据表和热敏电阻读数装置的详细信息)。 我必须包括任何带数值的温