Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从同一类中的另一个方法打印值_Java_Methods_Joptionpane - Fatal编程技术网

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

Java 从同一类中的另一个方法打印值,java,methods,joptionpane,Java,Methods,Joptionpane,我现在有一段代码,它显示整数是否为正/负、奇数/偶数、素数/复合数。我只想在调用kCheck方法后,将kctr的值打印到print函数中。for循环是正确的,但在调用kCheck后,它只打印0 import javax.swing.JOptionPane; public class FirstClass { public int kCheck(int num, int k, int kctr) { for(int l=1;l<num+1;l++){

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

import javax.swing.JOptionPane;

public class FirstClass
{

    public int kCheck(int num, int k, int kctr)
    {
        for(int l=1;l<num+1;l++){
            if(( num%l) == 0){
                kctr++;
                //System.out.println("" +kctr); Just to check if the loop is   corrrect
            }
        }
        return kctr;
    }

    public static void main (String args[])
    {
        CheckClass checker = new CheckClass();
        FirstClass checker2 = new FirstClass();
        int num = 0;
        int even = 0;
        int odd = 0;
        int negeven = 0;
        int negodd = 0;
        int k = 0;
        int kctr = 0;
        num = Integer.parseInt(JOptionPane.showInputDialog (null, "Input an integer.", JOptionPane.QUESTION_MESSAGE));

        boolean ch=true;

        while(ch){
            if(num%2 == 0 && num>0){
                ch=false;
                even = 1;
            }
            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 == 0 && num < 0){
                negeven = 1;
            }
            else if(num%2 == 1 && num < 0){
                negodd = 1;
            }
            break;
        }

        if(even == 1){
            checker.posEvenCheck(num);
        }
        if(odd == 1){
            checker.posOddCheck(num, kctr);
        }
        if(negeven == 1){
            checker.negEvenCheck(num);
        }
        if(negodd == 1){
            checker.negOddCheck(num);
        }
    }
}
import javax.swing.JOptionPane;
公务舱头等舱
{
公共int kCheck(int num、int k、int kctr)
{
对于(int l=1;l0){
ch=假;
偶数=1;
}
else if(num%2==1&&num>0){
checker2.kCheck(num,k,kctr);
奇数=1;
System.out.println(“+kctr”);/*只是为了检查循环是否正确,问题是否在这里。
它在方法kCheck上打印kctr的值,但是当它在这里时,它打印0*/
}
else if(num%2==0&&num<0){
负偶数=1;
}
else if(num%2==1&&num<0){
negodd=1;
}
打破
}
如果(偶数==1){
checker.posEvenCheck(num);
}
如果(奇数==1){
checker.posOddCheck(num,kctr);
}
如果(负偶数==1){
checker.negEvenCheck(num);
}
如果(负奇数==1){
checker.negOddCheck(num);
}
}
}

更改此部分代码:

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);//更改这部分代码:

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);//更改这部分代码:

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);//更改这部分代码:

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);//您没有说明当前遇到的问题。我怀疑这只是因为您试图调用实例方法而没有指定实例-如果您搜索收到的错误消息,您会发现许多类似的问题…您没有说明当前遇到的问题。我怀疑这就是问题所在您试图在不指定实例的情况下调用实例方法-如果您搜索收到的错误消息,您会发现许多类似的问题…您尚未说明当前遇到的问题。我怀疑您只是试图在不指定实例的情况下调用实例方法-如果您进行搜索对于您收到的错误消息,您会发现许多类似的问题…您没有说明当前遇到的问题。我怀疑这只是因为您试图调用实例方法而没有指定实例-如果您搜索收到的错误消息,您会发现许多类似的问题…谢谢!非常感谢我做了一切却得不到答案谢谢!谢谢!你是一个救生员我做了一切却得不到答案谢谢!谢谢!你是一个救生员我做了一切却得不到答案谢谢!谢谢!你是一个救生员我做了一切却得不到答案谢谢!