java计算插入数的能力
嘿,所以我需要计算一个插入到我第一个文本字段中的数字的系数,但当我点击按钮时,它给了我0。有人能帮我吗?非常感谢。我也不擅长java,所以如果你看到一些改进plz共享的东西:)java计算插入数的能力,java,numbers,Java,Numbers,嘿,所以我需要计算一个插入到我第一个文本字段中的数字的系数,但当我点击按钮时,它给了我0。有人能帮我吗?非常感谢。我也不擅长java,所以如果你看到一些改进plz共享的东西:) 修正如下您必须首先使用faculty方法初始化fac变量。请尝试以下操作: public void actionPerformed(ActionEvent e) { int temp = Integer.parseInt(textFiled1.getText());//keep in mind that if t
修正如下您必须首先使用faculty方法初始化fac变量。请尝试以下操作:
public void actionPerformed(ActionEvent e) {
int temp = Integer.parseInt(textFiled1.getText());//keep in mind that if the text is not a number this will give you an error
fac = faculty(temp);
textField2.setText("" + fac);
}
此外,您还必须修改教员方法:
TextField textField1, textField2;
Button btn;
static int fac;
public static int faculteit(int n) {
int temp = 1;//this is a temporal variable that we use to compute the output of this method
for (int i = 1; i <= n; i++)
{
temp*= i;
}
return temp;
}
textfieldtextfield1,textField2;
按钮btn;
静态int-fac;
公共静态int faculteit(int n){
int temp=1;//这是一个时间变量,我们用来计算这个方法的输出
对于(int i=1;i请将fac变量初始化为1。
将数字与0相乘,结果返回为0
内部faculteit方法和for循环之前
fac = 1;
您从不调用faculteit
,而且fac
未初始化,因此即使调用该方法,它仍将保持为0。“faculty”是否表示“factorial”?Viktor这段代码应该可以用,也许你还有一些其他的代码想给我们看?@Viktor把fac
insidefaculteit
method作为一个局部变量,并将其初始化为1。(例如,在该方法的开头加上int fac=1;
)@Eran是的,你是对的,我忘了提到那部分。