gpt4 book ai didi

java - 尝试在方法内调用方法

转载 作者:行者123 更新时间:2023-11-30 02:40:23 24 4
gpt4 key购买 nike

尝试在方法中调用方法。

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com