Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.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 - Fatal编程技术网

Java方法在返回值之前保持运行

Java方法在返回值之前保持运行,java,Java,我试图让我的程序在运行完最后一个for循环后返回值,但由于某种原因,它在for循环后多次返回。我在返回之前添加了一个System.print来测试正在发生的事情,它循环了很多次。第一个结果是我所期望的,但是循环导致它不正确。我怎样才能阻止这种事情发生 import java.lang.Math; import java.util.Scanner; public class RussianExpo { static int counter = 0; static int[]

我试图让我的程序在运行完最后一个for循环后返回值,但由于某种原因,它在for循环后多次返回。我在返回之前添加了一个System.print来测试正在发生的事情,它循环了很多次。第一个结果是我所期望的,但是循环导致它不正确。我怎样才能阻止这种事情发生

import java.lang.Math; 
import java.util.Scanner;

public class RussianExpo 
{
    static int counter = 0;
    static int[] intArray = new int[25];
    static int value = 1;

    public static int expomod(int a, int b, int n)
    {
        while(true)
        {
            if (b == 0)
            {
                intArray[counter] = 1;
                break;
            }
            if (b==1)
            {
                intArray[counter] = a;
                break;
            }

            if (b>1)
            {
                if((b & 1) == 0); 
                {
                    intArray[counter] = 1;
                    a = (a*a);
                    b = (int) Math.floor(b/2);
                    counter++;
                    expomod(a,b,n);
                }
                if((b & 1) == 1);
                {
                    intArray[counter] = a;
                    a = (a*a);
                    b = (int) Math.floor(b/2);
                    counter++;
                    expomod(a,b,n);
                }
            }
        }

        for (int i = 0; i < 25; i++)  
        {
            if(intArray[i]>0)
            {
                value = value*intArray[i];
                value = value % n;
            }
        }
        System.out.println(value);
        return value; 
    }

    public static void main(String[] args) 
    {
        Scanner SC = new Scanner(System.in);
        System.out.println("Enter a");
        int a = SC.nextInt();
        System.out.println("Enter b");
        int b = SC.nextInt();
        System.out.println("Enter n");
        int n = SC.nextInt();

        System.out.println(expomod(a,b,n));

        SC.close();
    }

}

在for循环中设置一个中断以退出循环如何?设置
值=值%n后的中断

可能是错误的,我不确定该程序的目的是什么,但我认为您的问题是递归调用

 if((b & 1) == 1);
                {
                    intArray[counter] = a;
                    a = (a*a);
                    b = (int) Math.floor(b/2);
                    counter++;
                    expomod(a,b,n); //This recalls the function!
                }
如果第一个值是正确的,那么在函数运行完它的整个过程后,它似乎就是答案,后续的值来自于返回,因为堆栈从递归调用返回,从expomod()函数内部删除expomod(a,b,n)调用可能会解决此问题

简短答复:


从expomod()内部删除对expomod(a、b、n)的所有调用,然后查看是否得到所需的结果。

您使用的是Scanner.nextInt(),但您没有处理新行。我相信您在expomod末尾得到多个打印,因为您是递归调用它的。@nomaker我知道我是递归调用它的,但为什么它会在System.out.println(value)之后再次运行;?我不要求它在那条线之后再运行。expomod应该怎么做?这段代码中有一些危险信号。不使用递归调用的返回值。此外,递归调用之间共享的静态变量也使我们很难摸索。@PaulRooney
 if((b & 1) == 1);
                {
                    intArray[counter] = a;
                    a = (a*a);
                    b = (int) Math.floor(b/2);
                    counter++;
                    expomod(a,b,n); //This recalls the function!
                }