gpt4 book ai didi

java - Java 中温度程序的类声明

转载 作者:行者123 更新时间:2023-12-02 06:27:33 25 4
gpt4 key购买 nike

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

  • 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

最佳答案

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

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

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

关于java - Java 中温度程序的类声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20386379/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com