分数计算器帮助JAVA

时间:2016-11-02 23:14:37

标签: java calculator

我正在研究一个小数计算器,下面是我到目前为止所做的。我被困在减少部分,但我不知道如何创建它来打印答案。此外,如果我的代码有任何其他问题,请告诉我。这个计算器应该输入如下:2/7和6/7以及类似+的运算符,这应该打印出来:1_1 / 7。感谢您的帮助。

import java.util.*;

public class calculator {
    String operator;
    String int1;
    String int2;
    public static int whole1;
    public static int whole2;
    public static int num1;
    public static int num2;
    public static int den1;
    public static int den2;
    public static void main (String []args) {
        System.out.println("Hello, I'm a fractional calculator. FracCalc for short.");
        Scanner everything = new Scanner(System.in);
            System.out.println("Enter your first number type:");
            String int1 = everything.nextLine();
            System.out.println("Enter your second number:");
            String int2 = everything.nextLine();
            System.out.println("Enter operation (+, *, -, /):");
            String operator = everything.nextLine();
    }

public static void fractionFormat (String int1, String int2, String operator) {
    if (int1.contains("_")) { //first number
        whole1 = Integer.parseInt(int1.substring(0, int1.indexOf("_")));
        num1 = Integer.parseInt(int1.substring(int1.indexOf("_") + 1, int1.indexOf("/")));
        den1 = Integer.parseInt(int1.substring(int1.indexOf("/") + 1));
        num1 = (whole1*den1)+num1;
    }
    else if (int1.contains("/")) {
        num1 = Integer.parseInt(int1.substring(0, int1.indexOf("/")));
        den1 = Integer.parseInt(int1.substring(int1.indexOf("/") + 1));
    }
    else {
        whole1 = Integer.parseInt(int1.substring(0));
        whole1 = num1;
        den1 = 1;
    }


    if (int2.contains("_")) { //second number
        whole2 = Integer.parseInt(int2.substring(0, int2.indexOf("_")));
        num2 = Integer.parseInt(int2.substring(int2.indexOf("_") + 1, int2.indexOf("/")));
        den2 = Integer.parseInt(int2.substring(int2.indexOf("/") + 1));
        num2 = (whole2*den2)+num2;
    }
    else if (int2.contains("/")) {
        num2 = Integer.parseInt(int2.substring(0, int2.indexOf("/")));
        den2 = Integer.parseInt(int2.substring(int2.indexOf("/") + 1));
    }
    else {
        whole2 = Integer.parseInt(int2.substring(0));
        whole2 = num2;
        den2 = 1;
    }
    math(operator, num1, den1, num2, den2);
}
public static void math(String operator, int num1, int den1, int num2, int den2) {

    if (operator.equals("+")) {
        System.out.println(addmath(num1, num2, den1, den2)); }
    else if (operator.equals("-")) {
        num2 = -1*num2;
        System.out.println(addmath(num1, num2, den1, den2)); }
    else if (operator.equals("*")) {
        System.out.println(multmath(num1, num2, den1, den2)); }
    else {
        System.out.println(multmath(num1, num2, den1, den2)); }
    }

public static String addmath(int num1 , int num2 , int den1 , int den2) {
    int newnum = (num1*den2) + (num2*den1);
    int newden = den1*den2;
    String answer = reduce(newnum, newden);;
    return answer; }

public static String multmath(int num1 , int num2 , int den1 , int den2) {
    int newnum = num1 * num2;
    int newden = den1 * den2;
    String answer = reduce(newnum, newden);
    return answer;  }

1 个答案:

答案 0 :(得分:0)

你错过了两件事,

对方法的要求,(由Manuel Mejias提出),

因此,您应该在主要方法结束时对此进行广告宣传: fractionFormat(int1,int2,operator);

此外,该方法减少“String answer = reduce(newnum,newden);”没有定义...你没有粘贴所有代码?