Results 1 to 11 of 11
 10042008, 06:43 PM #1
[SOLVED] How can I dynanically change math signs
Hi Gang:
What I'm looking for is a way to dynamically change the plus "+" or minus "" signs in my program (I searched the forum and didn't see anything similiar).
Explanation:
 If the condition is x, then use "+" in all math operations
 If the condition is y, then use "" in all math operations
I know that I can have two sections in the program, one that adds and the other that subtracts, but it would be easier (and more efficient) if could establish this from the begining since the two sections are the same except for the math operations.
Thanks
Chris
 10042008, 06:47 PM #2
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,371
 Blog Entries
 1
 Rep Power
 20
 10042008, 07:29 PM #3Senior Member
 Join Date
 Aug 2008
 Posts
 384
 Rep Power
 7
Indeed, I don't really see the problem here either...
Just use if statements or multiply some variables with 1...I die a little on the inside...
Every time I get shot.
 10042008, 07:35 PM #4
Agreed, but I'm trying to avoid repeating the same code twice where the only difference is the math sign. Something like (simple pseudo code):
Java Code:if (conditon = x) { mathSign = +; } else if (conditon = y) { mathSign = ; } result = 100 mathSign 10; //depending on the value of mathSign the answer //could be 90 or 110
Chris
 10042008, 07:56 PM #5
Using the ternary operator
Java Code:result = 100 + 10 * (conditon ? 1 : 1);
Java Code:int sign = 0; if (conditon) { sign = 1; } else { sign = 1; } result = 100 + 10 * sign;
Last edited by DarrylBurke; 10042008 at 08:01 PM. Reason: missed a code tag
 10042008, 08:29 PM #6
[SOLVED] How can I dynanically change math signs
Supamagier & Darryl.Burke... thanks for the responses. Multiplying by the Sign variable will do the job.
Thanks,
Chris
 10052008, 04:41 AM #7
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,371
 Blog Entries
 1
 Rep Power
 20
 10052008, 07:51 PM #8
OK... here we go again
I will try to be as explicit as possible:
Problem: I needed to be able to change the math sign (+ or ) depending on some established condition:
 If the condition is x, then use "+" in all math operations
 If the condition is y, then use "" in all math operations
What I didn't want to do was program two separate sections, one using the plus sign and the other using the minus sign, since the logic is the same in both sections.
Solution: Establishing a variable (sign variable) that contains a 1 or 1 (see sign variable used in Daryl Burkes example below) provides the solution I was looking for:
Java Code:int sign = 0; if (conditon) { sign = 1; } else { sign = 1; } //logic follows here... result = 100 + 10 * sign;
Thanks
 10062008, 09:10 AM #9
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,371
 Blog Entries
 1
 Rep Power
 20
You can use the ternary operator to define the sign as well.
 10062008, 05:03 PM #10Senior Member
 Join Date
 Aug 2008
 Posts
 384
 Rep Power
 7
ternary operator, what's that?
EDIT: nvmLast edited by Supamagier; 10062008 at 05:08 PM.
I die a little on the inside...
Every time I get shot.
 10072008, 04:07 AM #11
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,371
 Blog Entries
 1
 Rep Power
 20
Similar Threads

Math Class
By ritwik07 in forum New To JavaReplies: 2Last Post: 09142009, 04:06 PM 
need help with math for a new program
By gotenks05 in forum New To JavaReplies: 13Last Post: 09262008, 07:32 AM 
Math.Random
By Java Tip in forum Java TipReplies: 0Last Post: 11232007, 03:09 PM 
Help with math in java
By fernando in forum New To JavaReplies: 1Last Post: 08062007, 06:05 AM 
Date math
By orchid in forum New To JavaReplies: 2Last Post: 04182007, 07:01 AM
Bookmarks