[英]Error with line 20: Required 'Int' found Double
我在20号线时遇到问题,希望有人可以指出我做错了正确的方向。 我要做的只是显示摄氏温度以及它们各自的华氏温度。 摄氏温度从0-100递增,华氏温度从32-212递增。否则,距离完成还很遥远,我知道这仍然是一团糟。 我已经显示了数据,但是它位于一列中,而不是应该包含的两列中。
感谢您的想法和投入。
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package celsiustofarenheittable;
/**
*
* @author these
*/
public class CelsiusToFarenheitTable {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
double celsiusToFarenheit,farenheit,celsius = 0;
celsiusToFarenheit = celsiusToFarenheit(farenheit,celsius); // HERE!
System.out.printf("Celsius \t Farenheit \n", celsius,farenheit);
{
for (celsius = 0; celsius <= 100; celsius += 10)
{
if (celsius <= 100 )
System.out.println(celsius);
}
while (farenheit <= 100){
System.out.println(farenheit * 1.8 + 32);
farenheit = farenheit + 10;
}
}
}
public static double celsiusToFarenheit(int celsius)
{
double farenheit;
farenheit = math.round(celsius * 1.8 + 32);
return farenheit;
}
}
您真的很接近,从摄氏到华氏的实际转换是正确的。
所以,你的问题是,你通过两项doubles
, farenheit
及celsius
到你的方法celsiusToFarenheit(int celsius)
你可以从方法声明中看到,它仅需要int celsius
。 从这个摄氏温度值,您可以转换为华氏温度。
因此,例如:
int celsius = 10;
double fahrenheit = celsiusToFarenheit(celsius);
产生的华氏温度为50,这是可以预期的
celsiusToFarenheit
方法需要一个integer
参数,并且在第20行中提供了两个double
参数。
我的建议是使您的celsius变量与方法参数类型( int
)相匹配,并仅在第20行提供celsius变量:
celsiusToFarenheit(celsius)
有太多不必要的变量和其他您实际上不需要的东西。 我已经修改了您的代码,使其变得尽可能简单,请享受:
public class CelsiusToFarenheitTable {
public static void main(String[] args)
{
double fahrenheit = 32.0, celsius = 0.0;
System.out.printf("Celsius \t Farenheit \n", celsius,fahrenheit);
for (celsius = 10; celsius <=100; celsius+=10)
{
System.out.print(celsius);
System.out.print("\t");
System.out.println(celsius * 1.8 + 32);
fahrenheit = fahrenheit + 10;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.