[英]Rather than storing JOptionPane input as a variable and passing to a method. Can you input variable directly into method?
我試圖提示用戶使用 JOptionPane 從對話框中輸入兩個數字,然后調用一個方法來返回它們的值。
我正在嘗試使用 Integer.parseInt() 將 JOptionPane 返回的字符串值轉換為 integer 但我無法讓它工作。
任何提示我哪里出錯了?
謝謝!
/**
*
*/
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
解釋:
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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.