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;
}
}
}
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
為什麼會這樣???
謝謝!
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