如何在消息对话框中输出数组

时间:2013-11-01 19:24:56

标签: java swing user-interface

我编写了这个程序,用户输入了fibonachi序列的第n个术语。因此,如果我输入2,则会出现一条消息,表明Fib序列的第二项是1.我还想输出一个数组的消息,直到它们输入的术语。例如。用户输入的是10,将显示一条消息,显示fib序列中第10个字段的所有数字。问题是我不知道该怎么做。它给了我一个奇怪的数字,如@ 2235f23(类似的东西)。

我的代码:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.security.cert.X509Certificate;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class maincard extends JFrame{   

    JLabel label;
    JTextField txf;
    JButton calculate;

    public static void main(String[] args){

        new maincard();


    }

    public maincard(){

        thehandler handler = new thehandler();

        JPanel panel = new JPanel();

        this.setSize(460, 360);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        this.setTitle("Fibonachi Seqeuence");
        this.add(panel);

        label = new JLabel("Enter the nth term to find the value of the corresponding Fibonachi Sequence");
        label.setToolTipText("What are you waiting for? Enter a damn number!!");
        label.setVisible(true);
        panel.add(label);

        txf = new JTextField("Term");
        txf.setSize(40, 25);
        txf.addActionListener(handler);
        txf.setVisible(true);
        panel.add(txf);

        calculate = new JButton("Calculate");
        calculate.addActionListener(handler);
        calculate.setSize(40, 60);      
        calculate.setVisible(true);
        panel.add(calculate);





    }

    private class thehandler implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            if(e.getSource() == calculate){
                int fibTerm = Integer.parseInt(txf.getText());
                int answer;

                int[] x = new int[fibTerm];

                x[0] = 0;
                x[1] = 1;

                for(int y = 2; y<x.length; y++){
                    x[y] = x[y-1] + x[y-2];

                }

                answer = x[fibTerm-1];
                JOptionPane.showMessageDialog(null, "Sequence up to the " + fibTerm + "is: " + x);
                JOptionPane.showMessageDialog(null, "The term is: " + answer);
            }

        }
    }

}

哦和另一个问题,找到第n个术语很有效但我不明白为什么它在我写'answer = x [fibTerm-1]'时有效但在我写'answer = x [fibTerm]'时不起作用

2 个答案:

答案 0 :(得分:0)

使用 Arrays.asList

将其打印为列表(转换后)

答案 1 :(得分:0)

修改actionPerformed(ActionEvent e)方法:

@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == calculate) {
        int fibTerm = Integer.parseInt(txf.getText());
        int answer;
        ArrayList<Integer> answers = new ArrayList<Integer>();
        int[] x = new int[fibTerm];
        x[0] = 0;
        x[1] = 1;
        for (int y = 2; y < x.length; y++) {
            x[y] = x[y - 1] + x[y - 2];
            answers.add(x[y]);
        }
        answer = x[fibTerm - 1];
        JOptionPane.showMessageDialog(null, "Sequence up to the "
                + fibTerm + " is: " + answers.toString());
        JOptionPane.showMessageDialog(null, "The term is: " + answer);
    }
}