0

我是一个java初学者,试图制作一个可以接受混合数字和分数的计算器,而不是计算它只是将两者结合起来的值。(ex.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;
}

}

4

2 回答 2

0

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

例如

firstNumber 是 String 类型,如“1”

secondNumber 是 String 类型,如 "1/2"

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

if(!input.contains("/")){ return ("答案是 "+ firstNumber + secondNumber ); }

这里会返回一个字符串值“The answer is 11/2”

这就是您遇到的情况(例如 1 + 1/2 答案是 11/2)

于 2013-10-29T23:24:26.953 回答
0

好吧,除了您的代码中的其他问题,您的问题“1 + 1/2 The answer is 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)));
}
于 2013-10-29T23:17:01.483 回答