简体   繁体   English

Java 方法在返回值之前一直运行

[英]Java method keeps running before returning value

I'm trying to get my program to return the value after running through the last for loop, but for some reason it loops back multiple times after the for loop.我试图让我的程序在运行完最后一个 for 循环后返回该值,但由于某种原因,它在 for 循环后多次循环返回。 I added a System.print right before return to test what was happening, and it loops many more times.我在返回之前添加了一个 System.print 来测试发生了什么,它循环了很多次。 The first result is what I expected, but the looping causes it the be incorrect.第一个结果是我所期望的,但循环导致它不正确。 How can I stop this from happening?我怎样才能阻止这种情况发生?

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();
    }

}

Output:输出:

Enter a
30
Enter b
2
Enter n
35

25
30
15
15

How about setting a break within the for loop to exit the loop?如何在 for 循环中设置中断以退出循环? Set the break after value = value % n;value = value % n;之后设置中断value = value % n;

Could be wrong, I'm not sure what the intention of the program is, but I think your issue is the recursive calls可能是错的,我不确定该程序的意图是什么,但我认为您的问题是递归调用

 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!
                }

If the first value is correct then it would appear to be the answer after the function has run it's complete course, the subsequent values come from the returns as the stack returns from the recursive calls, removing the expomod(a,b,n) call from inside the expomod() function may fix this issue.如果第一个值是正确的,那么它似乎是函数运行完成后的答案,后续值来自返回,因为堆栈从递归调用中返回,删除了 expomod(a,b,n) 调用从 expomod() 函数内部可以解决这个问题。

Short answer:简短的回答:

remove any calls to expomod(a,b,n) from inside expomod() and see if you get the result you want.从 expomod() 内部删除对 expomod(a,b,n) 的任何调用,看看是否得到了想要的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM