【问题标题】:Array input numbers 0-2 and output corresponding letters a,b,c数组输入数字 0-2 并输出对应的字母 a,b,c
【发布时间】:2019-03-10 23:02:32
【问题描述】:

我已经创建了包含元素 ("A","B","C") 的数组 如果用户输入'0',则输出“A”到输出标签,例如,outputLabel.setText(array[0])。

当我输入正确的数字时,我只是在命令提示符中收到错误。对此的任何帮助将不胜感激。我已经正确创建了 gui。只是不确定数组和输出。

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GuiFrame extends JFrame implements ActionListener {

String[] stringArray = {"A", "B", "C"};
JTextField inputArea;
JLabel theOutputLabel;


public GuiFrame() {

    JPanel panel = new JPanel();
    JLabel label1 = new JLabel("Please enter the index of the array to 
output: ");
    JLabel outputLabel = new JLabel("Array index");
    JTextField userInput = new JTextField ();
    JButton inputButton = new JButton("Go");
    String inputFromUser = userInput.getText();

    Container contentPane = getContentPane();

    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
    panel.add(label1);
    panel.add(outputLabel);
    panel.add(userInput);
    panel.add(inputButton);

    inputButton.addActionListener(this);

    contentPane.add(panel);
    setSize(250, 250);
    setVisible(true);
    userInput.setSize(250,50);

    System.out.println(inputFromUser);

    String stringArray[] = new String[3];


  }


public static void main(String[] args){
    new GuiFrame();
}

@Override
public void actionPerformed(ActionEvent e) {

    String userInput = inputArea.getText();
try {
    do {
        if (e.getActionCommand().equals("0"))
            theOutputLabel.setText(stringArray[0]);
        if (e.getActionCommand().equals("1"))
            theOutputLabel.setText(stringArray[1]);
        if (e.getActionCommand().equals("2"))
            theOutputLabel.setText(stringArray[2]);
    }while(e.getActionCommand().equals("0") || e.getActionCommand().equals("1") || e.getActionCommand().equals("2"));

    System.out.println("You have entered a number that is outside of the range of the array index please try again");

    }

catch (ArrayIndexOutOfBoundsException arrayError){
    System.out.println("Array Index Out of Bounds");
    arrayError.printStackTrace();
     }

   }

}

【问题讨论】:

    标签: java arrays output


    【解决方案1】:

    您现在拥有的东西违背了使用数组的目的。想象一下,你必须对 Alphabet 的所有字母都这样做,你会添加 26 个条件吗?如果您有数千种选择怎么办?

    因此,而不是

        /** DON'T DO THIS */
    
        if (e.getActionCommand().equals("0"))
            theOutputLabel.setText(stringArray[0]);
        if (e.getActionCommand().equals("1"))
            theOutputLabel.setText(stringArray[1]);
        if (e.getActionCommand().equals("2"))
            theOutputLabel.setText(stringArray[2]);
    

    您应该根据索引解析输入并从数组中获取元素。

    /** DO THIS */ 
    
    int index = Integer.parseInt(e.getActionCommand());
    
    theOutputLabel.setText(stringArray[index]);
    

    如果输入不是有效整数,Integer.parseInt() 可能会抛出 java.lang.NumberFormatException,因此您必须为此添加一个 catch。

    如果您想让indexwhile 条件下可用于测试,请在do 块之前声明它而不进行初始化。

    【讨论】:

      【解决方案2】:

      嘿,除了@isapir 的建议之外,请检查您的代码中是否有少数地方会导致 NullPointerExceptions,例如:

      JTextField inputArea; // Not assigned will lead to NPE
      JLabel theOutputLabel; // Not assigned will lead to NPE
      String userInput = inputArea.getText(); // Because of inputArea unassigned this line will throw NPE for sure so fix that as well.
      

      所以我假设您在 cmdPrompt 中遇到的异常是 NPE 异常,因此最好先修复您的基本错误并正确检查您的构造函数代码。最后,在 SO 上发布问题之前,最好先分享异常详细信息。

      e.getActionCommand().equals("0") 此行不会给出您在弹出框中输入的内容。也检查这个而不是使用inputArea.getText() 会给你用户输入的数字。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多