2016-06-14 62 views
-1

嘿所以我需要计算一个数字的系数,我插入到我的第一个文本区域,但是当我点击按钮时,它给了我0。有人可以帮助PLZ吗?不胜感激。我也没有在Java中,所以如果你看到的东西,以提高PLZ份额:)java计算插入数字的能力

FIX向下跌破

+1

你永远不会调用'faculteit'和'fac'没有初始化,所以它会保持0,即使你调用该方法。 – Eran

+4

“教师”是指“阶乘”吗? – khelwood

回答

0

必须使用教师法first.Try这个初始化变量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; 
} 
+0

仍然给0:/ – Viktor

+0

@Viktor这段代码应该可以工作,也许你有一些其他的代码部分你想告诉我们? – theVoid

+0

@Viktor在'faculteit'方法内使'fac'成为一个局部变量并将其初始化为1(即在该方法的开头添加'int fac = 1;') – Eran

0

请初始化变量FAC为1 你用0乘以数字和它返回结果为0。

内faculteit方法和之前的循环,

fac = 1;