Android: Converting a set of functions into an equation -
i 99% sure cannot done, thought ask certain.
i attempting create application calculates required dice roll action in popular tabletop war game.
the following calculation in java
int x = ((wsattacker * 2) - wsdefender); int y = (wsattacker - wsdefender); string result; // calculation +5 if (x <= -1) { result = "5+"; } // calculation +4 else if (x >= 0 && y <= 0) { result = "4+"; } // calculation +3 else if (y > 0) { result = "3+"; } else { result = "error"; } return result;
now issue avoid copywriter infringement cannot mention name of game in application, , cannot hard code above calculation in app.
this means difficult tell potential user app do.
the solution can think of make application generic , allow user input calculation required in form of equation.
an equation can place anonymously on public board or similar.
therefore questions follows.
- is there way of going this?
- if no, possible condense above code single expression/ equationi.e. 1 removes if , else statements
to answer question 2:
result = test_condition_1 ? result2_if_true : (test_condition_2 ? result2_if_true : test3_or_result2);
you can build 'compound' test conditions way, , it's based upon ternary operators.
edit
ternary operators short-hand way of writing if..then..else
statments, , more information can found in wiki-link above. example of use below, can compile , run:
public class ternarytest { public static void main(string [] args){ int x = 14; int y = 5; string result = ( x <= 10 ) ? "less 10" : "more 10"; system.out.println("result is: " + result); } }
try running , see result change value of x
understand how works. it's possible extend include , else
replacing "more 10"
string.
Comments
Post a Comment