Java program for calculating fractions

后端 未结 3 1037
梦如初夏
梦如初夏 2021-01-22 18:06

The purpose of the program is to get two user inputs for a fraction, receive a operator from the user, and then to get two more user inputs for a second fraction. The program mu

3条回答
  •  北荒
    北荒 (楼主)
    2021-01-22 18:30

    UPDATE:

    Be aware that this is my no means complete and/or the best solution, but at least it should take you in the right direction. You will still have to reduce fractions and do some other tweaks.

    import java.math.BigInteger;
    import java.util.Scanner;
    
    public class FractionCalculator {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
    
            int n1;
            int n2;
            int d1;
            int d2;
            int n = 0;
            int d;
            char o;
    
            System.out.println("Enter a numerator for fraction 1: ");
            n1 = in.nextInt();
            System.out.println("Enter a denominator for fraction 1: ");
            d1 = in.nextInt();
    
            if (d1 > 0) {
                System.out.println();
            } else {
                System.out.println("Invalid denominator");
                System.exit(0);
            }
    
            System.out.println("Enter an operator: ");
            o = in.next().toCharArray()[0];
            System.out.println("Enter a numerator for fraction 2: ");
            n2 = in.nextInt();
            System.out.println("Enter a denominator for fraction 2: ");
            d2 = in.nextInt();
    
            if (d2 > 0) {
                System.out.println();
            } else {
                System.out.println("Invalid denominator");
                System.exit(0);
            }
    
            switch (o) {
                case '*':
                    n = n1 * n2;
                    d = d1 * d2;
    
                    break;
    
                case '/':
                    n = n1 * d2;
                    d = n2 * d1;
    
                    break;
    
                case '+':
                case '-':
    
                    d = gcd(d1, d2);
    
                    n1 *= d / d1;
                    n2 *= d / d2;
    
                    if(o == '+') {
                        n = n1 + n2;
                    }
                    else if(o == '-') {
                        n = n1 - n2;
                    }
    
                    break;
                default:
                    System.out.println("Illegal Operator: " + o);
                    return;
            }
    
            System.out.printf(" %d     %d     %d\n", n1, n2, n);
            System.out.printf("--- %c --- = ---\n", o);
            System.out.printf(" %d     %d     %d\n", d1, d2, d);
        }
    
        private static int gcd(int d1, int d2) {
            BigInteger gcd = new BigInteger(String.valueOf(d1)).gcd(new BigInteger(String.valueOf(d2)));
            return gcd.intValue();
        }
    }
    

提交回复
热议问题