我是一个java新手,写了一段代码,有关:摄氏度和华氏度之间的互相转化package method;import method.HuaShe;public class TestCToFandFToC {public static void main(String[] args){System.out.println("摄氏 华氏 华氏 摄氏");for(int i = 1;i <= 10;i++){double she1 = 40.0;double hua1 = cToF(she1);double hua2 = 120.0;double she2 = fToC(hua2);System.out.println(she1 + " " + hua1 + " " + hua2 + " " + she2);she1 -= 1;hua2 -= 10;}}}//问题:为何出现错误?我import了啊!!其中cToF和fToC是我另外写的函数,但是没有和上面代码段放在一起,而是新建了一个: package method;
public class HuaShe {
static double cToF(double c){
double ft = (9.0 / 5) * c + 32;
double f = (int)(ft * 100) / 100.0;
return f;
}
//--------------------------------------
static double fToC(double f){
double ct = (f - 32) / (9.0 / 5);
double c = (int)(ct * 100) / 100.0;
return c;
}
}然后运行主函数时cToF(she1);和fToC(hua2);报错:The method cToF(double) is undefined for the type TestCToFandFToC为什么会这样???谢谢!
7 回答

qq_花开花谢_0
TA贡献1835条经验 获得超7个赞
TestCToFandFToC 类中没办法直接调用 cToF(she1)和fToC(hua2)这两个方法,不管你是不是import
如果需要使用,可以
HuaShe test = new HuaShe (); double hua1 = test.cToF(she1);
,也可以
double hua1 = HuaShe.cToF(she1);
还可以import时,
static import method.HuaShe
添加回答
举报
0/150
提交
取消