2015-03-25 90 views
-2

需要一種方法來完成我的'高級'計算器。我一直在用循環進行頭腦風暴,但我很困惑自己。關於如何將操作數實現爲數字的任何想法,而不是僅僅嵌套一堆if語句和硬編碼。。實現多個操作數

import java.util.*; 

公共類計算器{

public static void main(String[] args) { 

    Scanner scan = new Scanner(System.in); 
    System.out.println("welcome to your personal calculator. is your problem simple or advanced(multiple calculations)\n" 
      + "Type 1 for simple or 2 for advanced"); 
    int choice = scan.nextInt(); 
    if(choice == 1){ 
     System.out.println("Simple equation"); 

     System.out.println("Enter the first number"); 
     int firstnum = scan.nextInt(); 
     System.out.println("Enter the second number"); 
     int secondnum = scan.nextInt(); 
     System.out.println("are you adding, subtracting multiplying or dividing? ENTER +,*,-, or /"); 
     String operand = scan.next(); 
     if(operand.equals("+")){ 
      int solution = firstnum + secondnum; 
      System.out.println(firstnum + operand + secondnum + "=" + solution); 
     } 
     else if (operand.equals("*")){ 
      int solution = firstnum * secondnum; 
      System.out.println(firstnum + operand + secondnum + "=" + solution); 
     } 
     else if(operand.equals("-")){ 
      int solution = firstnum - secondnum; 
      System.out.println(firstnum + operand + secondnum + "=" + solution); 
     } 
     else if(operand.equals("/")){ 
      int solution = firstnum/secondnum; 
      System.out.println(firstnum + operand + secondnum + "=" + solution); 
     } 
     else{ 
      System.out.println("Did not recognize operation type please restart program."); 
      return; 
     } 

     } 

    else if(choice == 2){ 
     System.out.println("Advanced equation"); 
     System.out.println("How many numbers are in your equation? max of 7\n" 
       + "operands are chosen by typing +,-,*,or /"); 
     int numofnumbers = scan.nextInt(); 
     if(numofnumbers == 2){ 
      System.out.println("enter first num"); 
      int firstnum = scan.nextInt(); 
      System.out.println("enter second num"); 
      int secondnum = scan.nextInt(); 
      System.out.println("enter operand"); 
      String operand = scan.next(); 
      if(operand.equals("+")){ 
       int solution = firstnum + secondnum; 
       System.out.println(firstnum + operand + secondnum + "=" + solution); 
      } 
      else if (operand.equals("*")){ 
       int solution = firstnum * secondnum; 
       System.out.println(firstnum + operand + secondnum + "=" + solution); 
      } 
      else if(operand.equals("-")){ 
       int solution = firstnum - secondnum; 
       System.out.println(firstnum + operand + secondnum + "=" + solution); 
      } 
      else if(operand.equals("/")){ 
       int solution = firstnum/secondnum; 
       System.out.println(firstnum + operand + secondnum + "=" + solution); 
      } 
      else{ 
       System.out.println("Did not recognize operation type please restart program."); 
       return; 
      } 

     } 
     else if(numofnumbers == 3){ 
      System.out.println("enter first num"); 
      int firstnum = scan.nextInt(); 
      System.out.println("enter second num"); 
      int secondnum = scan.nextInt(); 
      System.out.println("enter thir num"); 
      int thirdnum = scan.nextInt(); 
     } 
     else if(numofnumbers == 4){ 
      System.out.println("enter first num"); 
      int firstnum = scan.nextInt(); 
      System.out.println("enter second num"); 
      int secondnum = scan.nextInt(); 
      System.out.println("enter third num"); 
      int thirdnum = scan.nextInt(); 
      System.out.println("enter fourth num"); 
      int fourthnum = scan.nextInt(); 
     } 
     else if(numofnumbers == 5){ 
      System.out.println("enter first num"); 
      int firstnum = scan.nextInt(); 
      System.out.println("enter second num"); 
      int secondnum = scan.nextInt(); 
      System.out.println("enter third num"); 
      int thirdnum = scan.nextInt(); 
      System.out.println("enter fourth num"); 
      int fourthnum = scan.nextInt(); 
      System.out.println("enter fifth num"); 
      int fifthnum = scan.nextInt(); 
     } 
     else if(numofnumbers == 6){ 
      System.out.println("enter first num"); 
      int firstnum = scan.nextInt(); 
      System.out.println("enter second num"); 
      int secondnum = scan.nextInt(); 
      System.out.println("enter third num"); 
      int thirdnum = scan.nextInt(); 
      System.out.println("enter fourth num"); 
      int fourthnum = scan.nextInt(); 
      System.out.println("enter fifth num"); 
      int fifthnum = scan.nextInt(); 
      System.out.println("enter sixth num"); 
      int sixthnum = scan.nextInt(); 
     } 
     else if(numofnumbers == 7){ 
      System.out.println("enter first num"); 
      int firstnum = scan.nextInt(); 
      System.out.println("enter second num"); 
      int secondnum = scan.nextInt(); 
      System.out.println("enter third num"); 
      int thirdnum = scan.nextInt(); 
      System.out.println("enter fourth num"); 
      int fourthnum = scan.nextInt(); 
      System.out.println("enter fifth num"); 
      int fifthnum = scan.nextInt(); 
      System.out.println("enter sixth num"); 
      int sixthnum = scan.nextInt(); 
      System.out.println("enter seventh num"); 
      int seventhnum = scan.nextInt(); 
     } 
    } 
    else{ 
     System.out.println("you did not type one or two. please restart program."); 
     return; 
    } 

}}

+0

http://stackoverflow.com/questions/29224513/java-beginner-code-snippet-help-code-not-working我的這個問題也需要另一個答案。我有一個答案解決了它,但它仍然沒有正常工作,所以它會很好,如果我可以得到一些建議,這個問題也 – taintedpyro813 2015-03-25 03:26:55

+0

而不是限制你的程序非常具體的輸入,你應該考慮如何採取一個典型的數學表達式作爲一個字符串並解析/評估它。看看這裏http://stackoverflow.com/questions/13662001/java-string-to-math-equation – user1274820 2015-03-25 04:51:51

回答

1

我喜歡這個挑戰,因爲它是如此重要的是什麼電腦都在首位製成的計算公式!首先想到的是計算機本身如何執行這些操作。據我瞭解,計算機一次執行一項操作(你得到了正確的部分!),然後存儲結果並基本重新評估剩下的問題,就好像該操作已完成一樣。

SO 4 + 3 + 2

簡直變成

7 + 2

爲了做這種事情在Java中,我認爲這是最好的熟悉一些概念,如ArrayList和String.split。

您需要ArrayList來解決問題,而不是更傳統的數組,因爲問題的長度將在整個程序中發生變化,正如我們上面用4 + 3 + 2示例所看到的。所以如果你想用循環來循環訪問一個數組,但是數組需要越來越短,這可能會變得非常複雜。

ArrayList是一個很棒的對象,它爲我們提供了一個具有各種有用方法的動態「表單」數組。

你可以像這樣聲明一個ArrayList。

ArrayList<*variable type*> *Name* = new ArrayList<*same variable type*>(); 

然後可以使用一堆不同的方法,例如。新增將項目添加到列表的末尾(增加由1的長度),卸下襬臂到刪除項目(及其含有指數將長度減1)和替換Array [idx]的.get(idx)。

現在您將要將運算符存儲在與操作數相同的對象中,因爲您要維護它們的順序。爲了做到這一點,你最好的選擇就是把整個條目存儲爲一個字符串,就像一個句子一樣,然後你就可以把整個東西分開。這是String.split進來

你可以看看它的使用這個真棒項:

https://stackoverflow.com/a/13525053/4710112

使用此:

String.split("(?<=[-+*/])|(?=[-+*/])"); 

可以返回包含數組每個數字緊跟其運營商的順序。

SO 4 + 3 + 2

成爲

4,+ 3,+,2

現在,這是非常重要的!

您不能將從分割區返回的數組直接放入數組列表中! 您必須首先將其存儲在傳統數組中,然後使用for循環和ArrayList.add()將其「喂」到ArrayList中。

現在我不打算爲你解決這個問題,因爲我相信你會想知道你自己。但隨時打我!

哦,最後幾件事情。您將需要.parseToDouble將ArrayList中的字符串轉換爲雙精度,並將.toString將它們轉換回字符串並將它們放回到ArrayList中。

哦,你應該學會編寫和調用方法,將使你的生活在組織方面更容易。

例如在主代替:

double result; 
double a; 
double b; 
result = a+b 

你可以這樣做:

result = add(a,b); 

只要你寫的方法主要之下:

public double add(double x, double y){ 

double tempResult; 
tempResult = x + y; 
return tempResult; 
} 

可能看上去首先打字,但當你習慣於編寫這樣的方法時,你會發現你的整體代碼變得很短r和更短

+0

只是想補充說你的示例方法可以縮短爲返回x + y; – user1274820 2015-03-25 04:37:50