在Java中使用Switch的计算器

时间:2014-10-29 02:34:59

标签: java switch-statement calculator

我是Java编程的新手。我正在尝试制作分数计算器但是当我尝试运行程序时它会给我一个错误。错误与Switch语句有关,但我不知道发生了什么。

    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        System.out.println("Welcome to My FracCalc");
        boolean continueProcessing = true;
        while (continueProcessing) {
            System.out.println("Type an expression and press <enter>");

            String Operand1 = console.next();
            if (Operand1.equals("quit")) {
                continueProcessing = false;
                System.out.println("Good bye!");
                break;
            } else {
                Operand1 = parseFullNumber(Operand1);
            }

            String Operator = console.next();
            if (Operator.equals("quit")) {
                continueProcessing = false;
                System.out.println("Good bye!");
                break;
            } else if (Operator.equals("+") || Operator.equals("-") || Operator.equals("/") || Operator.equals("*")) {

            } else {
                throw new ArithmeticException();
            }

            String Operand2 = console.next();
            if (Operand2.equals("quit")) {
                continueProcessing = false;
                System.out.println("Good bye!");
                break;
            } else {
                Operand2 = parseFullNumber(Operand2);
            }


            System.out.println( Operand1 + " " +  Operator + " " + Operand2); 
            //System.out.println("First Fraction is: " + Operand1);
            //System.out.println("Operator is: " + Operator);
            //System.out.println("Second Fraction is: " + Operand2);


            float answer;
            System.out.println(Operator);
            switch (Operator) {
                case "+":
                    answer = Operand1 + Operand2;
                    break;

                case "-":
                    answer = Operand1 - Operand2;
                    break;

                case "*":
                    answer = Operand1 * Operand2;
                    break;

                case "/":
                    answer = Operand1 / Operand2;
                    break;

        }   
    }
}

    public static String parseFullNumber(String input) {
        int wholeNumber = 0;
        int numerator = 0;
        int denominator = 0;
        ;
        int underscoreId = input.indexOf('_');
        int slashId = input.indexOf('/');

        // Check for underscore "_" //
        if (underscoreId > -1) { 
            wholeNumber = Integer.parseInt(input.substring(0, underscoreId));
            numerator = Integer.parseInt(input.substring(underscoreId + 1, slashId));
            denominator = Integer.parseInt(input.substring(slashId + 1, input.length()));
        } else {
            if (slashId > -1) {
                // no underscore but there is a slash // 
                numerator = Integer.parseInt(input.substring(0, slashId));
                denominator = Integer.parseInt(input.substring(slashId + 1, input.length()));
            } else {
                // there is no underscore or slash // 
                wholeNumber = Integer.parseInt(input);
            }
        }

        return simplify(wholeNumber, numerator, denominator);
    }

    //simplifying fractions //
    public static String simplify(int wholeNumber, int numerator, int denominator) {
        // absolute values //
        int absNumerator = Math.abs(numerator);

        // factor if applicable //
        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;
        }

        // reduce if applicable //
        if (absNumerator > denominator) {
            int reduction = numerator / denominator;
            if (wholeNumber >= 0) {
                wholeNumber += reduction;
            } else {
                wholeNumber -= reduction;
            }
            numerator %= denominator;
        }

        // prints //
        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);
            }
        }
    }
}

这是我得到的错误:

Exception in thread "main" java.lang.Error: 
     Unresolved compilation problems: 
          Type mismatch:
              cannot convert from String to float The operator - is undefined for the argument type(s) java.lang.String, java.lang.String
              The operator * is undefined for the argument type(s) java.lang.String, java.lang.String
              The operator / is undefined for the argument type(s) java.lang.String, java.lang.String 
                  at FracCalcApp.main(FracCalcApp.java:53)

1 个答案:

答案 0 :(得分:3)

Operand1Operand2String(s)。您需要先解析它们才能执行算术运算。像,

double answer;
System.out.println(Operator);
switch (Operator) {
case "+":
    answer = Double.valueOf(Operand1) + Double.valueOf(Operand2);
    break;
case "-":
    answer = Double.valueOf(Operand1) - Double.valueOf(Operand2);
    break;
case "*":
    answer = Double.valueOf(Operand1) * Double.valueOf(Operand2);
    break;
case "/":
    answer = Double.valueOf(Operand1) / Double.valueOf(Operand2);
    break;
}

最后,按照惯例,Java变量应该以小写字母开头; operand1operand2operator