2011-11-17 27 views
0

一直試圖弄清楚這個問題,幾個小時以來一直令我頭腦發熱。(double,int)不能應用於(雙精度)

我有這是主要的方法:

public static void main(String [] args) 
    { 
     double payRate; 
     double grossPay; 
     double netPay; 
     int hours; 

     Scanner input = new Scanner(System.in); 

     System.out.println("Welcome to the Pay Roll Program"); 

     printDescription(); 

     System.out.print("Please input the pay per hour: "); 
     payRate = input.nextDouble(); 

     System.out.println("\nPlease input the pay per hour: "); 
     hours = input.nextInt(); 
     System.out.println("\n"); 


     netPay = computePaycheck(netPay); 

     System.out.println("The net pay is $" + (netPay)); 
     System.out.println("We hope you enjoyed this program"); 

     System.exit(0); 

和計算的netPay

public static double computePaycheck(double payRate, int hours) 
{ 


    double grossPay = computePaycheck(payRate*hours); 

    double netPay = (grossPay - (grossPay *.15)); 

    return netPay; 

} 

但我發現了在薪水錯誤「computePaycheck(雙,INT)的方法不能(雙)「

我有點理解這一點,但我不能爲我的生活找出補救措施。

+0

哪個IDE用於開發? – hanumant

回答

0

netPay = computePaycheck(netPay);

公共靜態雙computePaycheck(雙payRate,INT小時)

「computePaycheck(雙,INT)在薪水不能被應用到(雙)」

你的方法採用兩個參數,一個雙和一個int。

您只能與這兩個電話通話(您錯過了通話中的小時數)。

netPay = computePaycheck(payRate,hours);

double grossPay = payRate * hours;

0

在你computePaycheck方法,你有下面這行:

double grossPay = computePaycheck(payRate*hours); 

這傳遞了一個參數(的payRatehours產品)到computePaycheck功能,這需要兩個參數。它看起來像你的意思是說:

double grossPay = computePaycheck(payRate, hours); 

但你需要小心!這將導致你的程序無限重現!您將需要確定如何在不調用此函數的情況下計算總工資,因爲如果您在其內部遞歸調用它,則不會返回它的條件。

+0

我明白了!我只是做了 '公共靜態雙computePaycheck(雙payRate,INT小時) \t { \t \t \t \t \t雙grossPay = payRate *小時; \t \t \t \t double netPay =(grossPay - (grossPay * .15)); \t \t \t \t return netPay; \t \t \t}' – brandonscott

0

你的方法有兩個參數 - double payRateint hours,但你只指定double,當你在你的主要方法調用computePaycheck

目前尚不清楚你打算髮生什麼,但不匹配的參數應該讓你知道你的程序有什麼問題。

2

1)您正在調用帶有2個參數的函數,而只傳遞1。這會導致編譯錯誤。

2)當你從內部調用computePaycheck會循環並導致堆棧溢出。

+0

這很有道理,我能夠解決這個錯誤。謝謝! – brandonscott

0

computePaycheck方法的第一條語句使用單個參數(雙精度)調用computePaycheck,而computePaycheck使用2個參數(double和int)。這就是爲什麼你的代碼無法編譯。

如果您通過使用double grossPay = computePaycheck(payRate, hours);來「解決」這個問題,那麼它會編譯,但您將得到無限遞歸!你不只是想做double grossPay = payRate*hours;