package Fibonacci;
class Fibonacci
{
public static void main(String[]args) {
int a = 0;
int b = 1;
String input;
input = javax.swing.JOptionPane.showInputDialog("How many elements you want to print in a Fibonacci series");
int n = Integer.parseInt(input);
javax.swing.JOptionPane.showMessageDialog(null, a " " b " ");
int c;
for(int i = 2; i < n; i ) {
c = a b;
javax.swing.JOptionPane.showMessageDialog(null, c " ");
a = b;
b = c;
}
}
}
// Here is the code ? what can I change to display the output on only one dialogbox? Sorry I'm just new with learning java,
CodePudding user response:
You should first collect the data which you want to print in a DialogBox. Then you can print the data with the DialogBox (not in the for loop).
Take a look at following code.
import javax.swing.*;
public class main {
public static long fibonacci(int n){
long a = 0, b = 1;
for (int i = 0; i < n; i ) {
b = a (a = b);
}
return a;
}
public static void main(String... args){
int input = Integer.parseInt(JOptionPane.showInputDialog("Number of print elements"));
String fib = "";
for (int i = 0; i <= input; i ) {
fib = fib fibonacci(i) "\n";
}
JOptionPane.showMessageDialog(null, fib);
}
}