2014-10-29 77 views
-1

我是Java編程新手。我試圖做一個分數計算器,但是當我嘗試運行該程序時,它給了我一個錯誤。這個錯誤與Switch語句有關,但我不知道發生了什麼。在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

什麼是錯誤? – ipavl 2014-10-29 02:36:31

+0

開關語句存在編譯時錯誤 – Strategiger 2014-10-29 02:37:22

+0

您在我認爲的switch語句結尾缺少'}'。但發佈完整的錯誤逐字更有用。 – ipavl 2014-10-29 02:37:59

回答

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變量應該以小寫字母開頭; operand1,operand2operator

+0

謝謝,這真的很有幫助。此外,我通過我的代碼和小寫所有變量。 – Strategiger 2014-10-29 02:49:16

+0

另一個錯誤:鍵入的表達,並按下 1_1/2 + 1_1/2 1_1/2 + 1_1/2 + 異常在線程 「主」 java.lang.NumberFormatException:對於輸入字符串: 「1_1/2」 \t在sun.misc.FloatingDecimal.readJavaFormatString(來源不明) \t在java.lang.Double.valueOf(來源不明) \t在FracCalcApp.main(FracCalcApp.java:53) – Strategiger 2014-10-29 02:50:41

+0

正確的。 1_1/2不是浮點數。試試'1.5' – 2014-10-29 02:51:21