Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在消息对话框中输出数组_Java_Swing_User Interface - Fatal编程技术网

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

Java 如何在消息对话框中输出数组,java,swing,user-interface,Java,Swing,User Interface,我写了这个程序,用户输入fibonachi序列的第n项。所以,如果我键入2,就会出现一条消息,Fib序列的第二项是1。我还想输出数组的消息,直到它们键入的术语为止。例如,如果用户键入10,则会出现一条消息,显示fib序列中第10项之前的所有数字。问题是我不知道怎么做。它给了我一个像@2235f23这样的wierd数字(类似的东西) 我的代码: import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.

我写了这个程序,用户输入fibonachi序列的第n项。所以,如果我键入2,就会出现一条消息,Fib序列的第二项是1。我还想输出数组的消息,直到它们键入的术语为止。例如,如果用户键入10,则会出现一条消息,显示fib序列中第10项之前的所有数字。问题是我不知道怎么做。它给了我一个像@2235f23这样的wierd数字(类似的东西)

我的代码:

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

        }
    }

}
导入java.awt.BorderLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.security.cert.x509证书;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JOptionPane;
导入javax.swing.JPanel;
导入javax.swing.JTextField;
公共类主卡扩展JFrame{
JLabel标签;
JTextField-txf;
JButton计算;
公共静态void main(字符串[]args){
新主卡();
}
公共主卡(){
thehandler=新的thehandler();
JPanel面板=新的JPanel();
这个。设置大小(460360);
此.setDefaultCloseOperation(关闭时退出);
此.setLocationRelativeTo(空);
此.setVisible(true);
本集标题(“Fibonachi Sequeuence”);
本条添加(面板);
label=new JLabel(“输入第n个项以查找对应的Fibonachi序列的值”);
label.setToolTipText(“你在等什么?输入一个该死的数字!!”);
label.setVisible(true);
面板。添加(标签);
txf=新的JTextField(“术语”);
txf.setSize(40,25);
addActionListener(处理程序);
txf.setVisible(真);
面板。添加(txf);
计算=新按钮(“计算”);
calculate.addActionListener(处理程序);
计算。设置大小(40,60);
calculate.setVisible(true);
面板。添加(计算);
}
私有类处理程序实现ActionListener{
@凌驾
已执行的公共无效操作(操作事件e){
如果(如getSource()==计算){
int fiberm=Integer.parseInt(txf.getText());
int答案;
int[]x=新的int[fiberm];
x[0]=0;
x[1]=1;

对于(int y=2;y使用数组将其打印为列表(转换后)。asList

修改您的
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);
    }
}
@覆盖
已执行的公共无效操作(操作事件e){
如果(如getSource()==计算){
int fiberm=Integer.parseInt(txf.getText());
int答案;
ArrayList answers=新的ArrayList();
int[]x=新的int[fiberm];
x[0]=0;
x[1]=1;
对于(int y=2;y
更好的解决方案是Arrays.toString(array)。谢谢你。toString()方法是否只是将int答案转换成字符串?不,它将
ArrayList
对象转换成字符串。