Java中的分数乘法器自定义方法

时间:2018-11-10 16:23:34

标签: java oop

我正在使用JB IntelliJ IDEA并试图创建一个包含类Fraction和方法multiplie的程序,将两个分数相乘,并应返回并打印出如下所示的图形:“相乘结果:int / int”,但打印:“相乘结果:[I @ 7ea987ac”。

这是我的代码:

Main.java:

package com.company;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
     Scanner scan = new Scanner(System.in);
     System.out.print("Numerator1 = ");
     int num1 = scan.nextInt();
     System.out.print("Denomerator1 = ");
     int denom1 = scan.nextInt();
     System.out.print("Numerator2 = ");
     int num2 = scan.nextInt();
     System.out.print("Denomerator2 = ");
     int denom2 = scan.nextInt();
    System.out.print("Multiplie result: " + Fraction.multiplie(num1,denom1,num2,denom1));
    }
}

和Fraction.java:

package com.company;

public class Fraction {
    static int numerator;
    static int denominator = 1;
    static int numerator2;
    static int denominator2;
    static int numeratorsmultiplie;
    static int denominatorsmultiplie;
    static int[]multiplieresult = new int[2];
    public Fraction(int numerator, int denominator) {
        if (denominator == 0) {
            System.out.println("Denominator can't be zero. Choose another one.");
            return;
        }
        this.numerator = numerator * (denominator < 0 ? -1 : 1);
        this.denominator = Math.abs(denominator);
        normalization();
    }
    public int getNumerator() {
        return numerator;
    }
    public int getDenominator() {
        return denominator;
    }
    public void setNumerator(int numerator) {
        this.numerator = numerator;
    }
    public void setDenominator(int denominator) {
        if (denominator == 0) {
            System.out.println("Denominator can't be zero. Choose another one.");
            return;
        }
        if (denominator < 0) {
            this.numerator *= -1;
        }
    }
    private int gcd(int numerator, int denominator) {
        while (numerator != 0 && denominator != 0) {
            if (numerator > denominator) {
                numerator %= denominator;
            } else {
                denominator %= numerator;
            }
        }
        return numerator + denominator;
    }
    private void normalization() {
        int n = gcd(Math.abs(numerator), Math.abs(denominator));
        numerator /= n;
        denominator /= n;
    }
    public static int[] multiplie(int numerator, int denominator, int numerator2, int denominator2) {
        numeratorsmultiplie = numerator * numerator2;
        denominatorsmultiplie = denominator * denominator2;
        final int[] ret = {numeratorsmultiplie, denominatorsmultiplie};
        return ret;
    }
}

2 个答案:

答案 0 :(得分:1)

更改

System.out.print("Multiplie result: " + Fraction.multiplie(num1,denom1,num2,denom1))

System.out.print("Multiplie result: " + Arrays.toString(Fraction.multiplie(num1,denom1,num2,denom1)));

因为您的multiplie方法返回了int []

答案 1 :(得分:0)

删除“ Fraction.java”中的所有变量,但不包括:

private static int numerator; private static int denominator;

并将multiplie方法更改为:

static String multiply(int numerator, int denominator, int numerator2, int denominator2) { int numeratorsmultiply = numerator * numerator2; int denominatorsmultiply = denominator * denominator2; String numeratorsmultiplystr = Integer.toString(numeratorsmultiply); String denominatorsmultiplystr = Integer.toString(denominatorsmultiply); return numeratorsmultiplystr + " / " + denominatorsmultiplystr; }

还更改“ Main.java” 将行打印到:

System.out.print("Multiplie result: " + Fraction.multiply(num1, denom1, num2, denom2));