Here's my code:
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class DisplayGUI extends JFrame
{
public static void main(String args[]) throws Exception
{
ButtonFrame buttonFrame = new ButtonFrame();
buttonFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buttonFrame.setSize(275, 75);
buttonFrame.setVisible(true);
}
private JButton Fibonacci;
private JButton Factorial;
public DisplayGUI()
{
setLayout(new FlowLayout());
Factorial= new JButton("Factorial");
add(Factorial);
Fibonacci = new JButton("Fibonacci");
add(Fibonacci);
ButtonHandler handler = new ButtonHandler();
Factorial.addActionListener(handler);
Fibonacci.addActionListener(handler);
}
private class ButtonHandler implements ActionListener
{
int a=0,ctr=0,rep=1,x,b=0;
String strNum = JOptionPane.showInputDialog(null, "Input integer number: ", "Input", JOptionPane.QUESTION_MESSAGE);
int Num=Integer.parseInt(strNum);
public void actionPerformed(ActionEvent event)
{
Object source = event.getSource();
if(source == Factorial)
{
a=Num;
ctr=Num;
while (ctr>1)
{
ctr--;
a=a*ctr;
}
JOptionPane.showMessageDialog(null, "Factorial of "+Num+": "+a+"\n", "Factorial", JOptionPane.INFORMATION_MESSAGE);
}
if(source == Fibonacci)
{
ctr=1;
int n=0;
int fib=1;
if (Num==0)
{
JOptionPane.showMessageDialog(null, "0", "Fibonacci", JOptionPane.INFORMATION_MESSAGE);
}
else
{
while (fib<Num)
{
if (ctr==1)
{
JOptionPane.showMessageDialog(null, ""+fib+"", "Fibonacci", JOptionPane.INFORMATION_MESSAGE);
a=fib;
}
else if (ctr==2)
{
JOptionPane.showMessageDialog(null, "1", "Fibonacci", JOptionPane.INFORMATION_MESSAGE);
b=1;
}
else
{
fib = (Num-1)+(Num-2);
for(int ctr=0;ctr<=10; ctr++);
JOptionPane.showMessageDialog(null, ""+ctr+"", "Fibonacci", JOptionPane.INFORMATION_MESSAGE);
}
}
}
}
}
}
}
My problem here is that when I try the fibonacci button, it shows the output one by one.
ex. input 15
1 then OK
1 then OK
2 then OK
and so forth...
What I need is to output the whole series in one dialog box like this...
1 1 2... and so forth...
Can anyone tell me what to do