计算器混合数字

时间:2013-10-29 22:57:57

标签: java numbers calculator fractions mixed

我是一个java初学者,试图制作一个可以接受混合数字和分数的计算器,但不是计算它只是将两者结合起来的值。 (例1 + 1/2 答案是11/2 )`import java.util.Scanner; 公共类计算器{

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

System.out.println("Welcome to the Calc-O-Lator 9000\nthis calculator is able to\nadd, subtract, mulitiple, divide, and handle exponents of\nFRACTIONS\n\nenter 'quit' when done");
System.out.println("To input a mixed number use an underscore in addition with a slash(ex. 2_1/2), also provide a space between the first number and operator\n and the operator and the second number.");
Boolean on=true;
Scanner console=new Scanner(System.in);String firstNumber = Woith.next();
    if (firstNumber.equals("quit")) {
        on = false;
        System.out.println("goodbye");


    } else {
        firstNumber = parseFullNumber(firstNumber);
    }
    String operator = Woith.next();
    if (operator.equals("quit")) {
        on = false;
        System.out.println("goodbye");

    } else if (operator.equals("+") || operator.equals("-") || operator.equals("/") || operator.equals("*")) {
    } else {
        throw new ArithmeticException();
    }

    String secondNumber = Woith.next();
    if (secondNumber.equals("quit")) {
        on = false;
        System.out.println("goodbye");

    } else {
        secondNumber = parseFullNumber(secondNumber);
    }
    int wholeNumber = 0;
    int numerator = 0;
    int denominator = 0;
    System.out.println(calculate(operator ,  firstNumber, secondNumber, wholeNumber, numerator, denominator));
}
public static String parseFullNumber(String input) {
    int wholeNumber = 0;
    int numerator = 0;
    int denominator = 0;
    int underscoreIdx = input.indexOf('_');
    int slashIdx = input.indexOf('/');
    if (underscoreIdx > -1) { 
        wholeNumber = Integer.parseInt(input.substring(0, underscoreIdx));
        numerator = Integer.parseInt(input.substring(underscoreIdx + 1, slashIdx));
        denominator = Integer.parseInt(input.substring(slashIdx + 1, input.length()));
    } else {
        if (slashIdx > -1) {

            numerator = Integer.parseInt(input.substring(0, slashIdx));
            denominator = Integer.parseInt(input.substring(slashIdx + 1, input.length()));
        } else {

            wholeNumber = Integer.parseInt(input);
        }
    }
    return reduce(wholeNumber, numerator, denominator);

}

public static String reduce(int wholeNumber, int numerator, int denominator) {
    int absNumerator = Math.abs(numerator);
    if (absNumerator > 1) {
        int commonFactor = 1;
        for (int i = 2; i < Math.min(absNumerator, denominator); i++) {
            if (numerator % i == 0 && denominator % i == 0) {
                commonFactor = i;
            }
        }
        numerator /= commonFactor;
        denominator /= commonFactor;
    }
    if (absNumerator > denominator) {
        int reduction = numerator / denominator;
        if (wholeNumber >= 0) {
            wholeNumber += reduction;
        } else {
            wholeNumber -= reduction;
        }
        numerator %= denominator;
    }
    if (wholeNumber != 0) {
        if (numerator != 0) {
            return wholeNumber + "_" + numerator + "/" + denominator; 
        } else {
            return String.valueOf(wholeNumber);
        }
    } else {
        if (numerator != 0) {
            return numerator + "/" + denominator;
        } else {
            return String.valueOf(0);
        }
    }
}

public static String calculate(String input, String firstNumber,String secondNumber,int wholeNumber,int numerator,int denominator){
    if (input.contains ("+"))
    {
        if(!input.contains("/")){
            return ("The answer is "+firstNumber + secondNumber);
        }
        if (input.contains("/")){
            return ("The answer is "+(numerator*denominator)+(numerator*denominator)+"/"+(numerator*denominator));
        }
        if(input.contains("_")){
            return ("The answer is "+wholeNumber+numerator+"/"+denominator);
        }
        if(input.contains("-")){
            if(!input.contains("/")){
                return ("The answer is "+firstNumber + secondNumber);
            }
            if (input.contains("/")){
                return ("The answer is "+(numerator*denominator)+"/"+(numerator*denominator));
            }
            if(input.contains("_")){
                return ("The answer is "+wholeNumber+numerator+"/"+denominator);
            }
        if(input.contains("*")){
            if(!input.contains("/")){
                    return ("The answer is "+firstNumber + secondNumber);
                }
            if (input.contains("/")){
                    return ("The answer is "+(numerator*numerator)+"/"+(denominator*denominator));
                }
            if(input.contains("_")){
                    return ("The answer is "+wholeNumber+numerator+"/"+denominator);
                }
            }
        }
    }
    return input;
}

}

2 个答案:

答案 0 :(得分:0)

好吧,除了你的代码中的其他问题,你的问题“1 + 1/2答案是11/2”来自这里:

if (input.contains("/")){
    return ("The answer is "+(numerator*denominator)+(numerator*denominator)+"/"+(numerator*denominator));
}

您声明的输出正确,因为它是字符串连接。你真的想要浮动转换,所以试试这个:

if (input.contains("/")) {
    return ("The answer is "+(numerator*denominator)+((float)(numerator*denominator)/(numerator*denominator)));
}

答案 1 :(得分:0)

这是因为您对字符串值使用了“+”opeartion。

e.g。

firstNumber是一种String类型,如“1”

secondNumber是一种String类型,如“1/2”

在计算方法中,您可以使用以下方式打印答案

如果(!input.contains( “/”)){             return(“答案是”+ firstNumber + secondNumber );         }

这里将返回一个字符串值“ 答案是11/2“

这就是你所遇到的(ex.1 + 1/2答案是11/2)