gpt4 book ai didi

java - 摄氏度温度表错误

转载 作者:行者123 更新时间:2023-12-01 11:48:16 25 4
gpt4 key购买 nike

因此,我应该显示一个表格,该表格使用 for 循环和接受华氏温度作为参数的摄氏方法,以 0.5 增量提供从 94F 到 104F 的华氏温度到摄氏温度的转换。(这周我们刚刚学习方法)为什么我会收到此错误?这段代码的方向正确吗?

FahrenheitToCelsius.java:28: error: cannot find symbol
fhdeg, celdeg(fhdeg) );
^
symbol: method celdeg(double)
location: class FahrenheitToCelsius
1 error

FahrenheitToSelsius.java

/* PC 5.6 - Fahrenheit to Celsius 
-------------------------
Programmer: xxxxxxxxxxx
Date: xxxxxxxxxxxxxxxxx
-------------------------
This program will convert Fahrenheit to Celsius and display the
results.
*/
// ---------1---------2---------3---------4---------5---------6---------7
// 1234567890123456789012345678901234567890123456789012345678901234567890

public class FahrenheitToCelsius
{
public static void main(String[] args)
{
System.out.println("Temperature Conversion Table");
System.out.println("____________________________");
System.out.println(" Fahrenheit Celsius ");
}
public static double celsius(double fhdeg)
{

for (fhdeg = 0; fhdeg >=94; fhdeg+=0.5)
{
double celdeg = 0;
celdeg = 5.0/9 * fhdeg-32;
System.out.printf( " %5.1d %4.1d\n",
fhdeg, celdeg(fhdeg) );
}
}
}

最佳答案

表达式 celdeg(fhdeg) 表示您正在调用名为 celdeg 的方法,并传递名为 fhdeg 的参数。您收到错误是因为没有名为 celdeg() 的方法。

但是,根据问题的陈述,我猜您不需要创建这样的方法。相反,您只需迭代华氏度的范围并以摄氏度显示等效值。您的 for 循环可能是这样的:

public static void main(String[] args) {
System.out.println("Temperature Conversion Table");
System.out.println("____________________________");
System.out.println(" Fahrenheit Celsius ");

// From 94F to 104F, with increments of 0.5F
for (double fhdeg = 94.0; fhdeg < 104.5; fhdeg += 0.5) {
// Calculate degree in Celsius by calling the celsiusFromFahrenheit method
double celdeg = celsiusFromFahrenheit(fhdeg);
// Display degrees in Fahrenheit and in Celsius
System.out.printf( " %.2f %.2f\n", fhdeg, celdeg);
}
}

static double celsiusFromFahrenheit(double fhdeg) {
return (5. / 9.) * fhdeg - 32;
}

关于java - 摄氏度温度表错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28977641/

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