有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

而不是将JOptionPane输入存储为变量并传递给方法。你们能把变量直接输入到方法中吗?

我试图提示用户使用JOptionPane从对话框中输入两个数字,然后调用一个方法返回它们的值

我正在尝试使用integer将JOptionPane返回的字符串值转换为整数。parseInt()但我无法让它工作

有什么提示我哪里出了问题吗

谢谢

    /**
 * 
 */
package methodsWeekThree;

import javax.swing.JOptionPane;

/**
 * @author kylemoffett
 *
 */
public class ReturnMethodExcersiseOne {

    /**
     * @param args
     */
    public static void main(String[] args) {

        System.out.println("Please input num1 and hit enter: ");

        System.out.println("Please input num2 and hit enter: ");

        System.out.println(additionCircuit(Integer.parseInt(JOptionPane.showInputDialog("Input num1:"))),
                Integer.parseInt(JOptionPane.showInputDialog("Input num2:")));

    }// end main

    /**
     * This method adds two numbers and returns the value
     * 
     * @param num1 - first number to add
     * @param num2 - second number to add
     * @return - total of the addition
     */
    public static int additionCircuit(int num1, int num2) {

        int total;

        total = num1 + num2;

        return total;

    }// end additionCircuit

}// end class

共 (3) 个答案

  1. # 1 楼答案

    试试看:

    public class ReturnMethodExcersiseOne {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
    
            System.out.println("Please input num1 and hit enter: ");
            int num1 = Integer.parseInt(JOptionPane.showInputDialog("Input num2:"));
            System.out.println("Please input num2 and hit enter: ");
            int num2 = Integer.parseInt(JOptionPane.showInputDialog("Input num2:"));
            
            System.out.println(additionCircuit(num1, num2));
    
        }// end main
    
        /**
         * This method adds two numbers and returns the value
         * 
         * @param num1 - first number to add
         * @param num2 - second number to add
         * @return - total of the addition
         */
        public static int additionCircuit(int num1, int num2) {
    
            int total;
    
            total = num1 + num2;
    
            return total;
    
        }// end additionCircuit
    
    }// end class
    

    解释:

    1. 最好先存储JOptionPane值,然后再将其传递给additionCircuit方法,以避免括号问题
  2. # 2 楼答案

    括号的嵌套是错误的

    System.out.println(additionCircuit(Integer.parseInt(JOptionPane.showInputDialog("Input num1:")),
    Integer.parseInt(JOptionPane.showInputDialog("Input num2:"))));
    
  3. # 3 楼答案

    这里有一个替代方法,它只显示一次JOptionPane,而不是两次

    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    
    import javax.swing.JDialog;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.WindowConstants;
    
    public class ReturnMethodExcersiseOne {
        public static void main(String[] args) {
    
            JPanel panel = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            JLabel num1Label = new JLabel("num1");
            panel.add(num1Label, gbc);
            gbc.gridx = 1;
            JTextField num1TextField = new JTextField(6);
            panel.add(num1TextField, gbc);
            gbc.gridx = 0;
            gbc.gridy = 1;
            JLabel num2Label = new JLabel("num2");
            panel.add(num2Label, gbc);
            gbc.gridx = 1;
            JTextField num2TextField = new JTextField(6);
            panel.add(num2TextField, gbc);
            JOptionPane optionPane = new JOptionPane(panel);
            JDialog dlg = optionPane.createDialog("Prompt");
            dlg.addWindowListener(new WindowAdapter() {
                @Override
                public void windowDeactivated(WindowEvent e) {
                    int num1 = Integer.parseInt(num1TextField.getText());
                    int num2 = Integer.parseInt(num2TextField.getText());
                    System.out.println(additionCircuit(num1, num2));
                    ((JDialog) e.getSource()).dispose();
                }
                
                @Override
                public void windowClosing(WindowEvent windowEvent) {
                    int num1 = Integer.parseInt(num1TextField.getText());
                    int num2 = Integer.parseInt(num2TextField.getText());
                    System.out.println(additionCircuit(num1, num2));
                }
            });
            dlg.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            dlg.setSize(200, 200);
            dlg.setVisible(true);
        }// end main
    
        /**
         * This method adds two numbers and returns the value
         * 
         * @param num1 - first number to add
         * @param num2 - second number to add
         * @return - total of the addition
         */
        public static int additionCircuit(int num1, int num2) {
            int total;
            total = num1 + num2;
            return total;
        }// end additionCircuit
    }