如何在不同的if-else语句中使用相同的数组

时间:2016-02-09 14:15:08

标签: java arrays if-statement

我的代码有错误吗?因为每当我使用排序和查看按钮时,数组中显示的数字为零,如“0”,但是当我在if-else语句中放置/使用showMessageDialog时,我用来放一些数字是正确的..请帮帮我..对不起我的英语不好..

import java.util.Scanner;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Prog extends JFrame implements ActionListener{
    private JButton sort = new JButton("Sort");
    private JButton view = new JButton("View");
    private JButton enter = new JButton("Enter");
    private JButton exit = new JButton("Exit");
    private JLabel end = new JLabel("Assignment Passed by: Kevin Sagun");

    public Prog(){
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500,400);
        setResizable(true);
        setLocationRelativeTo(null);
        setTitle("Assignment po namin :)");
        setLayout(new FlowLayout()); 

        add(enter);
        add(sort);
        add(view);
        add(exit);
        add(end);

        sort.addActionListener(this);
        view.addActionListener(this);
        enter.addActionListener(this);
        exit.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e){
        Object source = e.getSource();

        int[] num=new int [5];
        int x, y, temp;
        String[] str = new String[5];

        if(source == enter){
            JOptionPane.showMessageDialog(null,"Enter Your Five Numbers: ");

            for(x=0;x < num.length;x++) {
                num[x]=JOptionPane.showInputDialog(null,"Number "+(x+1)+":");
                num[x]=Integer.parseInt(str[x]);
            }

            JOptionPane.showMessageDialog(null,num[0]+"IT "+num[1]+"IT "+num[2]+"IT "+num[3]+"IT "+num[4]+"IT ");

        }

        if(source == view){
            JOptionPane.showMessageDialog(null,num[0]+"IT "+num[1]+"IT       "+num[2]+"IT "+num[3]+"IT "+num[4]+"IT ");
        }

        if (source == sort) {
            for (y=0;y < num.length-1;y++) {    
                for (x=0;x < num.length-1;x++) {
                    if (num[x+1] < num[x]) {
                        temp=num[x];
                        num[x]=num[x+1];
                        num[x+1]=temp;}
                }
            }
        }

        if(source == exit){
            System.exit(0);
        }
     }

    public static void main(String[] args){
        Prog frame = new Prog();
    }   
}

4 个答案:

答案 0 :(得分:0)

每次单击“查看”按钮时,将再次触发整个事件,并重新创建所有本地实例!这就是为什么数组保持零或NULL值..

答案 1 :(得分:0)

source==view时,您的阵列中永远不会有元素,因为在显示阵列之前,您不会填充阵列。您只能在source==entersource==sort时填充数组,然后在下一个操作中重新初始化它。您必须在if(source == view)块中填充数组。

答案 2 :(得分:0)

当您声明int[]num=new int [5];时,您在方法中执行此操作意味着它是本地变量。您似乎正确地为您的线路分配了它:

num[x]=JOptionPane.showInputDialog(null,"Number "+(x+1)+":");

我猜你的意思是:

str[x]=JOptionPane.showInputDialog(null,"Number "+(x+1)+":");

为了保留更改,它需要成为类成员,这意味着您应该将它放在方法的外部(通常在顶部附近)。你的课程将成为:

public class Prog extends JFrame implements ActionListener{
    private int[] num=new int [5];

    // ...

    public void actionPerformed(ActionEvent e){
        Object source = e.getSource();

        int x, y, temp;
        String[] str = new String[5];
        // int[] num=new int [5];

        // ...
     }

     // ...
}

尝试一下,您就会看到更改被保留!

答案 3 :(得分:0)

我没有编译你的代码,但我认为这里有一个错误:

if(source == enter){
        JOptionPane.showMessageDialog(null,"Enter Your Five Numbers: ");
        for(x=0;x<num.length;x++){
            num[x]=JOptionPane.showInputDialog(null,"Number "+(x+1)+":");

应该是:

if(source == enter){
        JOptionPane.showMessageDialog(null,"Enter Your Five Numbers: ");
        for(x=0;x<num.length;x++){
            str[x]=JOptionPane.showInputDialog(null,"Number "+(x+1)+":");