The reason why the maximum number of pledges of the coding ligament 12 does not come out in Eclipse's maximum pledge number

Asked 2 years ago, Updated 2 years ago, 27 views

import java.util.*; public class area {

public static void main(String[] args) {
    // // TODO Auto-generated method stub
    double x,y,division;
    division=1;
    Scanner input = new Scanner(System.in);
    System.out.println ("Enter two integers");

    x=input.nextDouble();
    y=input.nextDouble();

    while(x%division!=0&&y%division!=0)
    {


    ++division;










    }
    System.out.println ("maximum number of pledges="+divisions);
}   

}strong text

Enter your code here

`

java

2022-09-22 12:26

2 Answers

Use System.out.println to see where the code starts and stops.

// import java.util.*;

public class area {

    public static void main(String[] args) {
        // // TODO Auto-generated method stub
        double x,y,division;
        division=1;
        // // Scanner input = new Scanner(System.in);
        // System.out.println ("Enter two integers");
        x=24.0; // Any x value for testing
        y=36.0; // Arbitrary y-value for testing

        while(x%division!=0&&y%division!=0)
        {
        System.out.println ("The current division has a value of " + division + ");"
        ++division;
        }
    }

}

You wrote a code that increases the division when both x and y are divided into divisions and the remainder is non-zero (non-mineral). There are some issues.

To change the data type to an integer type and increase the number of pledges to the maximum number of pledges

// import java.util.*;

public class area {

    public static void main(String[] args) {
        // // TODO Auto-generated method stub
        int x,y,division,gcd;
        division=1;
        // // Scanner input = new Scanner(System.in);
        // System.out.println ("Enter two integers");
        x=24; // Any x value for testing
        y=36; // Arbitrary y-value for testing
        gcd=1; // Initialize GCD containing the maximum number of pledges

        while(division<=x&&division<=y)
        {
            System.out.println ("The current division has a value of " + division + ");"
            if(x%division==0&&y%division==0)
            {
                System.out.println(division+)" is a common divisor of x and y.");
                gcd = division;
            }
            ++division;
        }
        System.out.println ("The maximum number of pledges for x and y is " + gcd + "";
    }

}

Do it like this

For reference, the method of dividing x and y directly into divisions and multiplying all of the abbreviations to obtain the maximum common divisor is

// import java.util.*;

public class area {

    public static void main(String[] args) {
        // // TODO Auto-generated method stub
        int x,y,division,gcd;
        division=2;
        // // Scanner input = new Scanner(System.in);
        // System.out.println ("Enter two integers");
        x=24; // Any x value for testing
        y=36; // Arbitrary y-value for testing
        gcd=1; // GCD == Maximum number of pledges

        while(division<=x&&division<=y)
        {
            System.out.println ("The current division has a value of " + division + ");"
            System.out.println ("The current value of x is " + x + "; the value of y is " + y + ");"
            if(x%division!=0||y%division!=0)
            {
                ++division;
            }
            else
            {
                x = x / division;
                y = y / division;
                gcd = gcd * division;
            }
        }
        System.out.println ("The maximum number of pledges for x and y is " + gcd + "";
    }

}

You can do it like this.


2022-09-22 12:26

No matter what value x, y has, Because it's divided by one.


2022-09-22 12:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.