如何使用 java 将假分数转换为带分数

How to convert a improper fraction to a mixed fraction using java

我正在尝试使用我为正在学习的课程开发的计算器将假分数转换为带分数。我被困在如何去做这件事上。这是我的代码。任何帮助将不胜感激

    package fractioncalc;

import java.util.Scanner;
public class fractioncalc2 {
    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        System.out.println("Please input your equation");

        String input = scan.nextLine().replaceAll(" ", "");
        System.out.println(input);
        boolean add = input.contains("+");
        boolean sub = input.contains("-");
        boolean multi = input.contains("*");

    //add
    if (add) {
        String Nume1 = input.substring(0,input.indexOf("/"));
        String Demo1 = input.substring(input.indexOf("/") + 1, input.indexOf("+"));
        System.out.println(Demo1);
        String Nume2 = input.substring(input.indexOf("+") + 1,input.lastIndexOf("/"));
        String Demo2 = input.substring(input.lastIndexOf("/") + 1, input.length());

        int num1 = Integer.parseInt(Nume1);
        int den1 = Integer.parseInt(Demo1);
        int num2 = Integer.parseInt(Nume2);
        int den2 = Integer.parseInt(Demo2);
        int numans = (num1*den2+num2*den1);
        int denans = (den1*den2);

        System.out.println("your answer is" +  numans + "/" + denans);
        }


    //subtract
    else if (sub) {
        String Nume1 = input.substring(0,input.indexOf("/"));
        String Demo1 = input.substring(input.indexOf("/") + 1, input.indexOf("-"));
        String Nume2 = input.substring(input.indexOf("-") + 1,input.lastIndexOf("/"));
        String Demo2 = input.substring(input.lastIndexOf("/") + 1, input.length());

        int num1 = Integer.parseInt(Nume1);
        int den1 = Integer.parseInt(Demo1);
        int num2 = Integer.parseInt(Nume2);
        int den2 = Integer.parseInt(Demo2);
        int numans = (num1*den2-num2*den1);
        int denans = (den1*den2);

        System.out.println("your answer is" +  numans + "/" + denans);
        }


    //multiply  
    else if (multi) {
        String Nume1 = input.substring(0,input.indexOf("/"));
        String Demo1 = input.substring(input.indexOf("/") + 1, input.indexOf("*"));
        String Nume2 = input.substring(input.indexOf("*") + 1,input.lastIndexOf("/"));
        String Demo2 = input.substring(input.lastIndexOf("/") + 1, input.length());

        int num1 = Integer.parseInt(Nume1);
        int den1 = Integer.parseInt(Demo1);
        int num2 = Integer.parseInt(Nume2);
        int den2 = Integer.parseInt(Demo2);
        int numans = (num1*den2*num2*den1);
        int denans = (den1*den2);

        System.out.println("your answer is" +  numans + "/" + denans);
        }


    //divide
    else {
        String b = input.substring(0, input.lastIndexOf("/"));
        String c = input.substring(input.indexOf("/")+1);
        String Nume1 = b.substring(0,input.indexOf("/"));
        String Demo1 = b.substring(input.indexOf("/") + 1, input.lastIndexOf("/")-2);
        String Nume2 = c.substring(input.indexOf("/") + 1,input.lastIndexOf("/")-2);
        String Demo2 = c.substring(input.lastIndexOf("/") - 1);

        int num1 = Integer.parseInt(Nume1);
        int den1 = Integer.parseInt(Demo1);
        int num2 = Integer.parseInt(Nume2);
        int den2 = Integer.parseInt(Demo2);
        int numans = (num1*den2);
        int denans = (den1*num2);

        System.out.println("your answer is:  " +  numans + "/" + denans);

    }
    }
}

考虑带分数有两部分:整数和分数。整数是 numerator 完全适合 denominator 的次数(这是 numerator / denominator 的底值)。分数的分子是本次除法的余数,分母不变。

因此,算法将是(伪代码):

Whole = Floor(Numerator / Denominator)
Part = Numerator % Denominator
Print(Whole + " " + Numerator + "/" + Denominator)

请注意此处使用的 % 符号。这称为模数,表示两个数相除的余数。 Java 使用相同的符号。

对于 Floor 的实现,查看 Java Math class.