需要一種方法來完成我的'高級'計算器。我一直在用循環進行頭腦風暴,但我很困惑自己。關於如何將操作數實現爲數字的任何想法,而不是僅僅嵌套一堆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;
}
}}
http://stackoverflow.com/questions/29224513/java-beginner-code-snippet-help-code-not-working我的這個問題也需要另一個答案。我有一個答案解決了它,但它仍然沒有正常工作,所以它會很好,如果我可以得到一些建議,這個問題也 – taintedpyro813 2015-03-25 03:26:55
而不是限制你的程序非常具體的輸入,你應該考慮如何採取一個典型的數學表達式作爲一個字符串並解析/評估它。看看這裏http://stackoverflow.com/questions/13662001/java-string-to-math-equation – user1274820 2015-03-25 04:51:51