Results 1 to 7 of 7
Thread: Simple question...I hope
- 01-07-2011, 05:23 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
Simple question...I hope
This is my code for a calculator, as you can see the method for"-" adds rather than takes away. I don't believe there is a -= rather than +=Java Code:public class Calculator2 { public static void main(String[] args){ double result = result_of("17+28-11"); System.out.println(result); } public static double result_of(String equation) { if(equation.contains("+")) { //equation.split not String.split String[] split_up_equation = equation.split("[+]"); double result = 0.0; for(int i=0;i<split_up_equation.length;i++) { //add the double value result += result_of(split_up_equation[i]); } return result; } if(equation.contains("-")) { String[] split_up_equation = equation.split("[-]"); double result = 0.0; for(int i=0;i<split_up_equation.length;i++) { result += result_of(split_up_equation[i]); } return result; } return Double.valueOf(equation); } }
Can anyone help me
- 01-07-2011, 05:27 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
- 01-07-2011, 05:28 PM #3
What happened when you tried? Keep in mind that you don't have to use the += operator; it's just a shortcut for doing something like this:
PS- In the future, you might want to use a more meaningful post title. People tend to ignore posts with generic titles like this one.Java Code:int x = 7; x = x + 2; // same as x += 2;
Edit- Too slow! But recommended reading: http://download.oracle.com/javase/tu...bolts/op1.htmlHow to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 01-07-2011, 05:29 PM #4
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
so
andJava Code:if(equation.contains("-")) { String[] split_up_equation = equation.split("[-]"); double result = 0.0; for(int i=0;i<split_up_equation.length;i++) { result -= result_of(split_up_equation[i]); } return result; } return Double.valueOf(equation);
should return 30?Java Code:double result = result_of("50-20");
- 01-07-2011, 05:32 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-07-2011, 05:37 PM #6
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 01-07-2011, 05:40 PM #7
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
Similar Threads
-
very simple Question
By arsenal4ever_11 in forum NetBeansReplies: 2Last Post: 05-27-2010, 08:51 PM -
hope to help me
By ABUALI in forum Advanced JavaReplies: 7Last Post: 05-27-2010, 04:43 PM -
some simple question?
By jperson in forum New To JavaReplies: 4Last Post: 05-03-2010, 05:32 PM -
hope there is an easier way.
By IYIaster in forum New To JavaReplies: 6Last Post: 10-14-2009, 07:26 AM -
Simple Question
By barusk in forum NetworkingReplies: 13Last Post: 03-04-2009, 07:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks