如何使用ArrayList从数字集中查找GCD和LCM

时间:2017-12-15 10:44:33

标签: java arraylist greatest-common-divisor lcm

我有一个问题,如何使用ArrayList查找GCD和LCM。现在我已经使用原始类型实现了Euklides算法。以下是我的模型和视图包。请教我如何改变模型中的方法?

型号:

    package Model;
import java.util.ArrayList; 
import java.util.List;

/**
 *
 * Class used to calculate two math formulas.
 *
 * @author anonymous
 * @version 1.24
 *
 */
public class LeastGreatestCommon {





    int result;     // result of counting Least Common Multiple  **
    int result2;    // result of counting Greatest Common Divisor **

    /**
     * Method counting Greatest Common Divisor from Euclidean algorithm
     *
     * @param a the first value of arguments
     * @param b the second value of arguments
     * @return returns the value of the largest common divisor for two input
     * values
     * @throws NegativeValueException where is one or more negative values
     */
    public int gcd(int a, int b) throws NegativeValueException {
        if (a < 0) {
            throw new NegativeValueException("You entered negative characters. Enter only posiitive numbers");
        }
        return b == 0 ? a : gcd(b, a % b);
    }

    /**
     * Method counting Least Common Multiple from Euclidean algorithm
     *
     * @param a the first value of arguments
     * @param b the second value of arguments
     * @return returns the value of the least common multiple for two input
     * values
     * @throws NegativeValueException where is one or more negative values
     */
    private int lcm(int a, int b) throws NegativeValueException {
        if (a < 0) {
            throw new NegativeValueException("You entered negative characters. Enter only posiitive numbers");
        }
        return a * b / (gcd(a, b));
    }

    /**
     * Method counting Greatest Common Divisor of n numbers
     *
     * @param r the number of arguments that will be used to calculate GCD
     * @param tab_tmp the value of the array
     * @return returns the value of the Greatest common divisor for all input
     * values from the input stream or defined arguments
     * @throws NegativeValueException where is one or more negative values
     */
   public int calculateGreatestCommonDivisor(int r, int number) throws NegativeValueException {

        List<Integer> getGreatestCommonDivisor = new ArrayList<Integer>();


        for (int i = 0 ; i < r ; ++i) {
         getGreatestCommonDivisor.add(i);
         result2 = gcd(result2, getGreatestCommonDivisor.get(i));  
        //  getGreatestCommonDivisor.remove(i);
        }
        return result2;
    }

    /**
     * Method counting Least Common Multiple of n numbers
     *
     * @param r the number of arguments that will be used to calculate LCM
     * @param tab_tmp the value of the array
     * @return returns the value of the Least Common Multiple for all input
     * values from the input stream or defined arguments
     * @throws NegativeValueException where is one or more negative values
     */
//    public int calculateLeastCommonMultiple(int r, int[] tab_tmp) throws NegativeValueException {
//
//        int i;                      // integer representing the i-element array 
//        int[] tab = tab_tmp;        // element of the array **
//        int r_tmp = r;              // number of elements in the array **
//        r = tab.length;
//        result = lcm(tab[0], tab[1]);
//
//        for (i = 1; i < r; i++) {
//            if (tab[i] < 0) {
//                throw new NegativeValueException("You entered negative characters. Enter only posiitive numbers");
//            }
//            result = lcm(result, tab[i]);
//        }
//        return result;
//    }
}

查看:

    package View;

import Model.LeastGreatestCommon;
import Model.NegativeValueException;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * Main view class
 *
 * @author anonymous
 * @version 1.23
 *
 */
public class View {

    /**
     * The main method of counting calculate based on the given arguments value
     * of GCD and LCM
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) throws NegativeValueException {

        int ResultOfTheMultiple;
        int ResultOfTheDivisor;
        //int r_tmp;

        LeastGreatestCommon leastGreatestCommon = new LeastGreatestCommon();  // Creating new object of LeastGreatestCommon class
//        Scanner scanner = new Scanner(System.in);
       ArrayList<Integer> getGreatestCommonDivisor = new ArrayList<>();

        if (args.length == 0) {   // Interaction with the user via the console

            int r;
            int number = 0;
            Scanner input = new Scanner(System.in);   // Creating Scanner object class associated with the input stream of object
            System.out.println("How many numbers will we count? ");
            r = input.nextInt();
            //int[] tab = new int[r];
            //System.out.println(r);
            for (int i = 0; i < r; i++) {
                System.out.println("Enter the number ");
                number = input.nextInt();

            }

            System.out.println("The array consists of " + r + " elements\n");
            for (int i = 0; i < r ; ++i) {
                System.out.print(number + " , ");
            }

            System.out.println("\n");

            try {
             //  getGreatestCommonDivisor.forEach(s -> System.out.println(s));
              ResultOfTheDivisor = leastGreatestCommon.calculateGreatestCommonDivisor(r, number);
//                for (Integer calculations : ResultOfTheDivisor) {
//                System.out.println(calculations);
//            }
            // ResultOfTheDivisor.forEach(System.out::println);
//ResultOfTheMultiple = leastGreatestCommon.calculateLeastCommonMultiple(r, tab);

                System.out.println("The Greatest Common Divisor of these numbers is " + ResultOfTheDivisor);
            //    System.out.println("The Least Common Multiple of these numbers is " + ResultOfTheMultiple);
            } catch (NegativeValueException ex) {
               //  System.out.println(e);
               System.out.println(ex.getMessage());
            }
        }

//        if (args.length > 1) {  // Interaction with the user using arguments in the project properties
//
//            System.out.println("Arguments were entered");
//            System.out.println("\n");
//            int[] tab_tmp = new int[args.length];
//            r_tmp = Integer.parseInt(args[0]);
//
//            for (int i = 0; i < args.length; i++) {
//                tab_tmp[i] = Integer.parseInt(args[i]);
//            }
//
//            try {
//                ResultOfTheDivisor = leastGreatestCommon.calculateGreatestCommonDivisor(r_tmp, tab_tmp);
//                ResultOfTheMultiple = leastGreatestCommon.calculateLeastCommonMultiple(r_tmp, tab_tmp);
//                System.out.println("The Greatest Common Divisor of these numbers is " + ResultOfTheDivisor);
//                System.out.println("The Least Common Multiple of these numbers is " + ResultOfTheMultiple);
//            } catch (NegativeValueException ex) {
//                System.out.println(ex.getMessage());
//            }
//        }
    }
};

问题是由于在方法CalculateGreatestCommonDivisor中没有得到任何正常结果。你能看看这个吗?

0 个答案:

没有答案