尝试在方法中调用方法。
public static void main(String[] args) {
int sq,cu=0;
//user input 1
sq=Integer.parseInt(JOptionPane.showInputDialog(
"Enter value to be squared"));
//user input 2
cu=Integer.parseInt(JOptionPane.showInputDialog(
"Enter value to be cubed"));
//results
JOptionPane.showMessageDialog(null, sqd(sq));
JOptionPane.showMessageDialog(null, cbd(cu));
}
public static String sqd(int sq){
int sqd=sq*sq;
//sq computation
return sq+" squared is "+sqd;
}
public static String cbd(int cu,int sqd){
int cbd;
cbd=sqd*cu;
//cu computation
return cu+" cubed is "+cbd;
} }
在cbd中调用sqd值,但是
JOptionPane.showMessageDialog(null, cbd(cu));
阻止我这样做,当我运行它时它总是给我一个错误。
最佳答案
您的cbd(int cu, int sqd)
方法需要2个输入参数,您仅使用一个参数调用它 cbd(cu)
两个选择:
1 - 使用 1 个参数重写您的 cbd
方法,然后您可以使用 cbd(cu);
public static String cbd(int cu){
return cu + " cubed is " + (cu*cu*cu);
}
2 - 在使用它时写入它的第二个参数:
cbd(cu,cu*cu);
关于java - 尝试在方法内调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41919755/