0

这段代码编译得很好,但是当我运行它时,它会按预期要求我的两个数字,然后就坐在那里,根本不做任何事情。我已经在 Internet 上搜索并为此工作了一整天。我终于屈服并寻求帮助。

是它没有自动循环备份的问题吗?10个小时后,我什么也没找到。

import java.util.Scanner;

public class EA
{
    public static void main (String[] args)
    {
        // get first integer from user
        Scanner input = new Scanner(System.in);
        System.out.println("Please enter the larger integer: ");
        int I;
        I = input.nextInt();

        // get second integer from user
        System.out.println("Please enter the smaller integer: ");
        int J;
        J = input.nextInt();

        //resolve the issue of zero
        while(J<1)
        {
            System.out.println("Can not divide by zero!");
            System.out.println("Please enter new smaller integer: ");
            J = input.nextInt();

            //do the calculations
            while(J>0)
            {
                int Remainder;
                Remainder = I % J;

                while(Remainder>0)
                {
                    I = J;
                    J = Remainder;

                    return;

                }
                System.out.println("GCD is" + J);
            }
        }
    }
}
4

5 回答 5

2

SJuan 提到 return 打破了循环,这是真的,但即使它已修复,还有一些其他问题:

  • 内部 while 永远不会结束(无限循环)
  • 结果将存储在J- 而不是I
  • System.out.println("GCD is " + I);应该印在外边外边!

你的程序的“心脏”应该这样做:

    // we get here with valid values stored in I,J
    int Remainder  = I % J;
    //do the calculations
    while(Remainder>0)
    {
        I = J;
        J = Remainder;
        Remainder  = I % J;
    }
    System.out.println("GCD is " + J);
于 2013-09-22T22:07:29.937 回答
2

Among other things already mentioned, you are confusing while with if. You have put your algorithm logic inside a while loop that only runs if the first input is bad.

// get first integer from user
Scanner input = new Scanner(System.in);
System.out.println("Please enter the larger integer: ");
int I;
I = input.nextInt();

// get second integer from user
System.out.println("Please enter the smaller integer: ");
int J;
J = input.nextInt();

//resolve the issue of zero
while(J<1)
{
    // You never reach here under ordinary conditions
}
于 2013-09-22T22:11:56.387 回答
0

欧几里得算法有一个缺点,因为两个输入都应该是非零来计算最大公约数。但是,如果您想在输入之一为零('0')时找出 GCD,请稍微调整一下逻辑。当其中一个输入为零时,GCD 为 1,并且“a”应大于“b”以计算 GCD。检查下面的片段:

    if (a < b) {
        int temp = a;
        a = b;
        b = temp;
    }
    if (b == 0) {
        System.out.println("1");
    } else {
        while (b != 0) {
            r = a % b;
            a = b;
            b = r;
        }
于 2014-05-21T20:21:48.267 回答
0

循环中间的return将结束执行。

这个

 while(Remainder>0)
 {
     I = J;
     J = Remainder;

    return; <------- THIS IS THE RETURN THAT BREAKS ALL

 }

所以它没有到达System.out.println.

更新:另外,你input.nextInt()J. 可能从您的描述来看,它一直在等待您输入第三个整数。

于 2013-09-22T22:05:50.227 回答
0

There are more than 1 error: the return in the while, the algorithm and the brackets of the first while.

1) When you resolve the issue of zero, the brackets of the while must be closed suddenly after you re-assign the value of the variable J.

while (J < 1) {
    System.out.println("Can not divide by zero!");
    System.out.println("Please enter new smaller integer: ");
    J = input.nextInt();
}

2) The algorithm for computing the gcd is the following:

function gcd(a, b)
    while b ≠ 0
       t := b
       b := a mod t
       a := t
    return a

Here is the correct version of your code:

public static void main(final String[] args) {
    // get first integer from user
    final Scanner input = new Scanner(System.in);
    System.out.println("Please enter the larger integer: ");
    int I;
    I = input.nextInt();

    // get second integer from user
    System.out.println("Please enter the smaller integer: ");
    int J;
    J = input.nextInt();

    // resolve the issue of zero
    while (J < 1) {
        System.out.println("Can not divide by zero!");
        System.out.println("Please enter new smaller integer: ");
        J = input.nextInt();
    }
    // do the calculations
    while (J != 0) {
        int Remainder;
        Remainder = I % J;
        I = J;
        J = Remainder;
    }
    System.out.println("GCD is" + I);

}
于 2013-09-22T22:11:39.457 回答