2014-02-24 35 views
-1

我想在分數之間做一個計算器,並且如果用戶輸入+, - ,*或/它將對應於該情況。這是我的代碼至今:輸入一個特殊字符,然後在開關櫃中使用

import java.util.Scanner; 
public class calculator 
{ 
    public static void main(String[] args) 
    { 
     Scanner input = new Scanner(System.in); 
     String x,y; 


     System.out.println("Enter first fraction in a/b form: "); 
     x = input.nextLine(); 

     System.out.println("Enter operation: "); 
     char z = input.next().charAt(0); 

     System.out.println("Enter second fraction in c/d form: "); 
     y = input.nextLine(); 

     String aString = x.substring(0,1); 
     String bString = x.substring(4,5); 
     String cString = x.substring(0,1); 
     String dString = x.substring(4,5); 

     int a = Integer.parseInt(aString); 
     int b = Integer.parseInt(bString); 
     int c = Integer.parseInt(cString); 
     int d = Integer.parseInt(dString); 


     int answer = 0; 
     switch (z) 
     { 
      case '+': 
       answer = (a/b) + (c/d); 
       break; 
      case '-': 
       answer = (a/b) - (c/d); 
       break; 
      case '*': 
       answer = (a/b) * (c/d); 
       break; 
      case '/': 
       answer = (a/b) /(c/d); 
       break; 
      default: 
       System.out.println("ERROR"); 
       break;    
     } 
     System.out.println("Answer = " + answer);  
    } 
} 

輸出應該

Enter first fraction in a/b form: 
1/2 
Enter operation: 
+ 
Enter second fraction in c/d form: 
2/5 
answer = 9/10 
+2

您遇到的問題是什麼?錯誤的輸出?錯誤信息?請分享。 – rgettman

+2

特別是,這裏沒有實際*問題*的跡象...... –

+0

您的交換機很好,@AJ。指出了一個邏輯錯誤,但是你有一個更大的問題,那就是輸出一個你需要做的分數[fraction arithmetic](http://www.sheboygan.uwc.edu/developmental-math/BAW/three/lesson03熱媒)。你可以用double來完成計算,所以你不會以0結束,但會得到一個十進制數。 – Radiodef

回答

1

的問題是在這裏

String aString = x.substring(0,1); 
String bString = x.substring(4,5); 
String cString = x.substring(0,1); 
String dString = x.substring(4,5); 

a=cb=d

這兩個線

String cString = x.substring(0,1); 
String dString = x.substring(4,5); 

應該是abcdz

String cString = y.substring(0,1); 
String dString = y.substring(4,5); 

之前switch打印的值,那麼你會知道你的正確性。

還有一件事是,你正在做int變量的劃分,所以結果將只在int

我建議你改變類型的abcdanswerdouble和使用Double.parseDouble()轉換stringdouble

+0

確實如此,但OP的代碼有多個問題。如他們的輸出顯示他們期望一個分數,但「答案」是作爲一個整數計算的。 – Radiodef

+0

@Radiodef是的,這是正確的。讓我在回答中包含這一點。 –

+0

謝謝!我做了更改@AJ說過,但我得到這個作爲我輸入'+':線程「主」的異常java.lang.StringIndexOutOfBoundsException:字符串索引超出範圍:1在java.lang.String.substring(String.java :1907)at calculator.main(calculator.java:33) – user3348064

0

這個問題是如此適合OOP,一個額外的類:

public class Q { 

    public final int numerator; 
    public final int denominator; 

    public static Q valueOf(String representation) { 
     Pattern pattern = Pattern.compile("([-]?\\d+) *[/:] *([-]?\\d+)"); 
     Matcher matcher = pattern.matcher(representation.trim()); 
     if (!matcher.matches()) { 
      throw new IllegalArgumentException(
       "Not a quotient (like '3/4'): " + representation); 
     } 
     int num = Integer.parseInt(matcher.group(1)); 
     int den = Integer.parseInt(matcher.group(2)); 
     return new Q(num, den); 
    } 

    public Q(int num, int den) { 
     if (den < 0) { 
      den = -den; 
      num = -num; 
     } 
     int c = gcd(Math.abs(den), Math.abs(num)); 
     denominator = den/c; 
     numerator = num/c; 
    } 

    @Override 
    public String toString() { 
     return numerator + "/" + denominator; 
    } 

    public Q add(Q rhs) { 
     int c = gcd(denominator, rhs.denominator); 
     int den = (denominator/c) * rhs.denominator; 
     int num = numerator * (rhs.denominator/c) 
       + rhs.numerator * (denominator/c); 
     return new Q(num, den); 
    } 

    public Q mult(Q rhs) { 
     int den = denominator * rhs.denominator; 
     int num = numerator * rhs.numerator; 
     return new Q(num, den); 
    } 

    public static int gcd(int x, int y) { 
     assert x >= 0 && y >= 0; 
     while (x != y) { 
      if (x > y) { 
       x -= y; 
      } else { 
       y -= x; 
      } 
     } 
     return x; 
    } 
} 

使用這個類的幾個小問題的代碼部分消失。

相關問題