2015-06-21 135 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