如何在单个对话框(选项)中打印出所有数字?

How can I print out all numbers in a single DialogBox (JOPTION)?

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;
}
   
}
}
  

// 这是代码?我可以更改什么以仅在一个对话框中显示输出?抱歉,我刚开始学习 java,

您应该首先在对话框中收集要打印的数据。 然后您可以使用 DialogBox 打印数据(不在 for 循环中)。

看看下面的代码。

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);
    }
}