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

Rather than storing JOptionPane input as a variable and passing to a method. Can you input variable directly into method?

我正在尝试提示用户使用 JOptionPane 从对话框中输入两个数字,然后调用一个方法来 return 它们的值。

我正在尝试使用 Integer.parseInt() 将字符串值 JOptionPane returns 转换为整数,但我无法让它工作。

我哪里出错了?

谢谢!

    /**
 * 
 */
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

尝试:

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 方法之前存储它以避免括号问题。

你的括号嵌套是错误的。

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

这是另一种方法,它只显示一次 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
}