而不是将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
# 1 楼答案
试试看:
解释:
JOptionPane
值,然后再将其传递给additionCircuit
方法,以避免括号问题李># 2 楼答案
括号的嵌套是错误的
# 3 楼答案
这里有一个替代方法,它只显示一次
JOptionPane
,而不是两次