2015-06-21 131 views
0

我正在爲我的Java類做一個問題,我希望你能指出我在正確的方向,或給我一個提示。循環成一個變量

程序主要使用我在主程序外編寫的方法。

該程序需要在JOptionPane中輸入變量,並將它們放入方法中。

該方法採用這些變量,並通過'while'循環運行它們,並將結果返回給main。

我遇到的問題是,獲取返回值回到主輸出。

我想有

return fin; 

回到主,所以它可以輸出。

問題的一部分,是我必須使用不同的數據類型,特別是float和double,我似乎無法正確解析。

的代碼如下:

public static double Compound(int time,double rate,float init) 
{ 
    double fin,capital; 
    int counter; 
    fin=capital; 
    capital=0; 
    counter=0; 
    while (counter<time) 
    { 
     capital=init+(rate/100.0)*init;   
     counter++;   
    } 
    return fin; 



} 

我知道我搞砸了某個地方,但我不能告訴哪裏。

編輯編輯編輯

我設法讓代碼大部分工作。現在我只需將變量插入上面的代碼中。

下面的代碼是我用來進入上面的代碼。

指導員希望在一個窗口中輸入所有內容。

將輸入引入方法會讓我感動,我知道它。

總之,這裏的代碼的其餘部分:

import javax.swing.JOptionPane; 
import javax.swing.JTextField; 
public class Pr50 
{ 
public static void main(String[] args) 
{ 
    JTextField time=new JTextField(); 
    JTextField interest=new JTextField(); 
    JTextField initial=new JTextField(); 
    Object[] fields={ 
     "Time to Maturity", time, 
     "Interest rate", interest, 
     "Initial deposit", initial 
     }; 
    JOptionPane.showInputDialog(null, fields); 


    int x; 
    double fin,rate; 
    float init; 
    fin=money.Compound(5,10,5500); 
} 
} 

編輯編輯編輯

我得到了該問題的居多,有點,不,真的。

我通過JOptionPane輸出運行程序,並且我得到了gobbeldygook。

import javax.swing.JOptionPane; 
import javax.swing.JTextField; 
public class Pr50 
{ 
public static void main(String[] args) 
{ 
    JTextField time=new JTextField(); 
    JTextField interest=new JTextField(); 
    JTextField initial=new JTextField(); 
    Object[] fields={ 
     "Time to Maturity", time, 
     "Interest rate", interest, 
     "Initial deposit", initial 
     }; 
    JOptionPane.showInputDialog(null, fields); 

    JOptionPane.showMessageDialog(null, "The value of the fund is: "+initial); 


    /*int x; 
    double fin,rate; 
    float init; 
    fin=money.Compound(5,10,5500);*/ 
} 
} 

我真的很想知道如何解決這個問題。

編輯編輯編輯

我需要從輸入窗口得到了變數,但我得到一個錯誤消息,指出「JText不能轉換成int類型。「

下面的代碼,在它所有的邪惡的榮耀:

主營:被徵召

import javax.swing.JOptionPane; 
import javax.swing.JTextField; 
public class Pr50 
{ 
public static void main(String[] args) 
{ 
    fin=money.Compound(time,interest,initial); 
    JTextField time=new JTextField(); 
    JTextField interest=new JTextField(); 
    JTextField initial=new JTextField(); 
    Object[] fields={ 
     "Time to Maturity", time, 
     "Interest rate", interest, 
     "Initial deposit", initial 
     }; 
    JOptionPane.showInputDialog(null, fields); 

    JOptionPane.showMessageDialog(null, "The value of the fund is: "+initial); 


    int x; 
    double fin,rate; 
    float init; 
    fin=money.Compound(time,interest,initial); 
} 
} 

方法:

public class money 
{ 
public static double Compound(int time,double rate,float init) 
{ 
    return init*Math.pow(1+(rate/100), time); 
} 
} 
+0

我真的不知道是什麼你在問這裏...是編譯錯誤還是運行時錯誤? – John3136

+0

你不應該返回資本而不是fin? –

+0

如果我放置另一半的程序會有幫助嗎?這只是給我一個問題的部分。 –

回答

0

從你的問題,我想你想得到方法Compound返回的值。

使用JOptionPane獲得變量time,rateinit後,只需在主方法內調用Compound方法即可。例如:

double compound = Compound(time, rate, init); 
System.out.println("The answer is " + compound); 
+0

教師說在程序中使用'float'數據類型作爲初始值。 –

+0

'init'仍然在上面浮動。方法簽名決定了它的參數類型必須是 –

+1

正如@BenKnoble所說的,除非你的教師希望你明確指定,否則'init'仍然是「float」。也許就像'fin = money.Compound(5,10,5500f)';請注意,「f」 – avonnadozie

0

有很多奇怪的東西在代碼事,但在我看來,這是你正在嘗試做的事:

public float compound(int time, double rate, float init) 
{ 
    return init * Math.pow(1 + (rate/100), time); 
} 
+0

init必須是浮點數據類型。 –

+0

然後它也會返回一個浮點數。除此之外,除了某些邊緣情況下的某些精度損失之外,它並不重要。 – wvdz