Java如何在数组中存储JOptionPane值

Java如何在数组中存储JOptionPane值,java,arrays,Java,Arrays,嘿,我有一个任务,要求我创建一个带有菜单系统的迷你程序,菜单系统有选项1和2。选项1要求我使用JOptionPane来输入和存储使用数组(而不是arraylist)的人的姓名和薪水。之后,选项2将显示人员姓名和工资列表。但是,我不确定如何将用户输入从JOptionPane存储到数组中,然后再显示它。任何帮助都将不胜感激!谢谢大家! public class HR{ public static void getName(){ String[] name= new

嘿,我有一个任务,要求我创建一个带有菜单系统的迷你程序,菜单系统有选项1和2。选项1要求我使用JOptionPane来输入和存储使用数组(而不是arraylist)的人的姓名和薪水。之后,选项2将显示人员姓名和工资列表。但是,我不确定如何将用户输入从JOptionPane存储到数组中,然后再显示它。任何帮助都将不胜感激!谢谢大家!

    public class HR{  
    public static void getName(){
        String[] name= new String[20];
        for (int count=0;count<1;count++){
            name[count]= JOptionPane.showInputDialog(null,"Please enter employee's name:", JOptionPane.QUESTION_MESSAGE);
        } 
    } 
    public static void getSalary(){
        String[] salary= new String[20];
        for (int count=0;count<1;count++){
            salary[count]= JOptionPane.showInputDialog(null,"Please enter employee's salary:", JOptionPane.QUESTION_MESSAGE);
        } 
    } 
公共类HR{
公共静态void getName(){
字符串[]名称=新字符串[20];

对于(int count=0;count,在这里使用单个
字符串
数组
可能并不理想,因为它在每个位置仅容纳一个
字符串
,因此您必须执行类似的操作*:

String arr[] = new String[20];
arr[0] = JOptionPane.showInputDialog(null, "Please enter employee's name:", JOptionPane.QUESTION_MESSAGE);
arr[1] = JOptionPane.showInputDialog(null, "Please enter employee's salary:", JOptionPane.QUESTION_MESSAGE);

JOptionPane.showMessageDialog(null, "Name is: " + arr[0] + " and salary is " + arr[1]);
如果20是您的大小,您可以使用循环,添加10个姓名和薪水条目,然后打印出来

for (int i = 0; i < arr.length; ++i) {
    arr[i] = JOptionPane.showInputDialog(null, "Please enter employee's name:", JOptionPane.QUESTION_MESSAGE);
    arr[++i] = JOptionPane.showInputDialog(null, "Please enter employee's salary:", JOptionPane.QUESTION_MESSAGE);
}

for (int i = 0; i < arr.length; ++i) {
    JOptionPane.showMessageDialog(null, "Name is: " + arr[i] + " and salary is " + arr[++i]);
}
将阵列定义为:

Person arr[] = new Person[20];
循环并设置您的值:

for (int i = 0; i < arr.length; ++i) {
    Person p = new Person();
    p.setName(JOptionPane.showInputDialog(null, "Please enter employee's name:", JOptionPane.QUESTION_MESSAGE));
    p.setSalary(JOptionPane.showInputDialog(null, "Please enter employee's salary:", JOptionPane.QUESTION_MESSAGE));
    arr[i] = p;
}
for(int i=0;i
并打印:

for (int i = 0; i < arr.length; i++) {
    JOptionPane.showMessageDialog(null, "Name is: " + arr[i].getName() + " and salary: " + arr[i].getSalary());
}
for(int i=0;i

*根据您的示例代码

运行此代码时会发生什么情况?是否有错误?@notyou否没有错误。那么,问题是什么?您当前的代码是否在数组中正确存储了姓名和薪资,而您不确定如何打印它们?@notyou否我当前的代码没有在数组中存储姓名和薪资,我也不确定是否有错误如何在JOptionPane.showMessageDialog.Thank中查看它!但是,由于我需要连续添加多个条目,这是否仍然有效?因为arr[0]和arr[1]是硬编码的。不是这样的。不;您需要一个循环并使用索引来代替硬编码的位置。但是,对于单个字符串数组来说,这不是一个理想的方案,您可能需要一个2d数组,或者更好的是一个映射或列表。@MonetWalkens我在这里给您提供了一个更好的解决方案。好的,没有问题,您将这段代码放在t中是一样的他把你放在想要的位置!刚刚测试过。很抱歉,我没有在我的帖子中明确说明,但是JOptionPane运行了20次,这不是我想要的。我需要一个系统,在这个系统中,我可以添加一个员工的名字,然后添加薪水,循环到主菜单,然后再次添加一个员工的名字和薪水。让我们总结一下,你需要的是添加员工的名字和薪水n个员工n次,但没有弹出工作窗格n次?我有一个菜单系统,有两个选项。第一个选项是添加员工姓名和薪资,第二个选项是查看员工姓名和薪资。当我添加员工姓名和薪资时,工作窗格会依次弹出。添加姓名和薪资后工资,它会循环回到主菜单,我可以通过按选项2来显示结果。但是,如果我想添加更多员工的姓名和工资,我可以再次按选项1并重复整个过程。您可以为每个员工使用数组列表,而不是使用两个字符串数组。此数组列表将存储员工name and salary您需要做的是在main中只创建2个JoptionPane,并在每次获取员工姓名和薪水时循环20次将其发送到get name&get salary函数,但请注意将数组列表指示为全局,以免每次调用这2个函数时都从索引0中添加,或者ypu可以直接调用l 1用于存储姓名和工资的函数
for (int i = 0; i < arr.length; i++) {
    JOptionPane.showMessageDialog(null, "Name is: " + arr[i].getName() + " and salary: " + arr[i].getSalary());
}
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

import com.sun.accessibility.internal.resources.accessibility;

import javax.swing.JButton;

public class stas {

    private JFrame frame;
    private JTextField textField;
    private JTextField textField_1;
    public static String salary_1="";
    public static String name_1="";

    public static String []name=new String[20];
    public static String []salary=new String[20];
    public static int counter=0;
    public static void get_name(String a)
{
    name[counter]=a;


}
    public static void get_salary(String a)
{
    salary[counter]=a;


}


    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    stas window = new stas();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public stas() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JLabel lblNewLabel = new JLabel("Enter Employe Name");
        lblNewLabel.setBounds(10, 47, 143, 20);
        frame.getContentPane().add(lblNewLabel);

        textField = new JTextField();
        textField.setBounds(198, 44, 86, 20);
        frame.getContentPane().add(textField);
        textField.setColumns(10);
        textField.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
        name_1=textField.getText();


            }
        });

        JLabel lblNewLabel_1 = new JLabel("Enter Employe Salary");
        lblNewLabel_1.setBounds(10, 119, 114, 17);
        frame.getContentPane().add(lblNewLabel_1);

        textField_1 = new JTextField();
        textField_1.setBounds(198, 116, 86, 20);
        frame.getContentPane().add(textField_1);
        textField_1.setColumns(10);
        textField_1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                 salary_1=textField_1.getText();



            }
        });

        JButton btnNewButton = new JButton("Add Employe");
        btnNewButton.setBounds(168, 175, 116, 23);
        frame.getContentPane().add(btnNewButton);
        btnNewButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                get_salary(salary_1);
                get_name(name_1);
                textField_1.setText("");
                textField.setText("");
                JOptionPane.showMessageDialog(null,"employe addedd");


            }
        });
       }
     }