从同一类中的另一个方法打印值

时间:2015-07-03 08:01:53

标签: java methods joptionpane

我现在有这个代码,它打印整数是正/负,奇/偶,素数/复合。我只想在调用kCheck方法后将kctr的值打印到print函数。 for循环是正确的,但在调用kCheck后它只打印0。

m(expr: String)

1 个答案:

答案 0 :(得分:3)

更改此部分代码:

else if(num%2 == 1 && num>0){
    checker2.kCheck(num, k, kctr);
    odd = 1;
    System.out.println("" +kctr); /*Just to check if the loop is correct and the problem is here. 
                                    It prints the value of kctr on the method kCheck but when it comes to here, it prints 0.*/
}

要:

else if(num%2 == 1 && num>0){
    kctr = checker2.kCheck(num, k, kctr); // <- Assign the return value of the method to kctr
    odd = 1;
    System.out.println("" +kctr);
}