分数十进制(反之亦然)转换器

时间:2015-10-19 00:44:16

标签: java decimal fractions

我有两部分代码,第一部分是将分数转换为十进制,第二部分是将十进制转换为分数。

但是,我必须将两段代码组合在一起,我不知道。我希望它将输入检测为双精度或分数,并将其转换为另一个。

import java.util.*;

public class ExcerciseEleven  {

    public static void main (String[] args) {
        Scanner sc = new Scanner (System.in);

        System.out.println("Enter Numerator: ");
        int numerator = sc.nextInt();
        System.out.println("Enter Denominator: ");
        int denominator = sc.nextInt();
        if (denominator == 0) {
            System.out.println("Can't divide by zero");
        }
        else {
            double fraction = (double)numerator / denominator;
            System.out.println(fraction);
        }
    }
}


public class Fractions {

    public static void main(String args[]) {

        double decimal;
        double originalDecimal;
        int LIMIT = 12;
        int denominators[] = new int[LIMIT + 1];
        int numerator, denominator, temp;
        int MAX_GOODNESS = 100;

        // Get a number to be converted to a fraction
        if (args.length == 1) {
            decimal = Double.valueOf(args[0]).doubleValue();
        } else {
            // No number was given, so just use pi
            assert args.length == 0; 
            decimal = Math.PI;
        }
        originalDecimal = decimal;

        // Display the header information
        System.out.println("-------------------------------------------------------");
        System.out.println("Program by David Matuszek");
        System.out.println("Input decimal number to be converted: " + decimal);
        System.out.println();

        // Compute all the denominators
        System.out.println("All computed denominators:");
        int i = 0;
        while (i < LIMIT + 1) {
            denominators[i] = (int)decimal;
            System.out.print(denominators[i] + "  ");
            decimal = 1.0 / (decimal - denominators[i]);
            i = i + 1;
        }
        System.out.println();
        System.out.println();

        // Compute the i-th approximation
        int last = 0;
        while (last < LIMIT) {

            // Print out the denominators used in this computation
            System.out.print("Using these " + (last + 1) + " denominators: ");
            for (int j = 0; j <= last; j++) {
                System.out.print(denominators[j] + "  ");
            }
            System.out.println();

            // Initialize variables used in computation
            numerator = 1;
            denominator = 1;
            temp = 0;

            // Do the computation
            int current = last;
            while (current >= 0) {
                denominator = numerator;
                numerator = (numerator * denominators[current]) + temp;
                temp = denominator;
                current = current - 1;
            }
            last = last + 1;

            // Display results
            double value = (double)numerator/denominator;
            int goodness = denominators[last];
            double error = 100 * Math.abs(value - originalDecimal) / originalDecimal;

            System.out.print("fraction = " + (int)numerator + "/" +
                             (int)denominator);
            System.out.print(", value = " + value);
            System.out.print(", goodness = " + goodness);
            System.out.println(", error = " + (int)error + "%");
            System.out.println();

            // Exit early if we have reached our goodness criterion
            if (Math.abs(goodness) > MAX_GOODNESS) break;
        }        
    }
}

1 个答案:

答案 0 :(得分:0)

如果我在一个提示符下完成所有操作,我会创建两个静态方法Fraction.TryParse(),我会使用内置的Double.TryParse(),如果decimal.TryParse返回true那么你实际上有小数。如果它返回false,那么你有一个Fraction,因此你必须使用你在Fraction.TryParse()中传递给Decimal.TryParse()的相同字符串。当然,您需要在Fraction.TryParse()方法中进行一些健全性检查。提示符可能如下所示:

Enter Decimal/Fraction: 3.14
Enter Decimal/Fraction: 1 + 1/2
Enter Decimal/Fraction: 1 1/2
Enter Decimal/Fraction: 1 (1/2)

你知道,如果你想在一行中使用这一切,你需要一些方法来分隔字符,如空格或括号,或者只是一个数学上准确的+符号。如果它全部在一行上,它也会简化你的程序,因为你没有多个提示一个对象。 &#34; 1(1/2)&#34;输入在技术上并不是数学上准确的,但你可以看到数据应该是如何构建的,你只是在数学上不能用这个提示。

这里我使用的是一半分数,你的实现没有混合数字实现,但你可以输入1/2或者其他东西,只是常规分数。