Results 1 to 4 of 4
  1. #1
    doejohn is offline Member
    Join Date
    Jun 2010
    Posts
    4
    Rep Power
    0

    Default A clever way of doing this ... (avoiding a LOT of if-else statements)

    Hi, currently my code looks like this:

    Java Code:
    // Adjust x aim point
    if(xWind < 0.50) {
    	offsetX = 1;
    } else if(xWind < 0.70) {
    	offsetX = 1;
    } else if(xWind < 0.80) {
    	offsetX = 2;
    } else if(xWind < 1.00) {
    	offsetX = 5;
    } else {
    	offsetX = 7;
    }
    // Adjust y aim point
    if(yWind < 0.50) {
    	offsetY = 14;
    } else if(yWind < 0.70) {
    	offsetY = 16;
    } else if(yWind < 0.80) {
    	offsetY = 17;
    } else if(yWind < 1.00) {
    	offsetY = 18;
    } else {
    	offsetY = 19;
    }
    However, offsetX and offsetY should both depend on both yWind and xWind. So I need to combine the two, so that I can adjust both the X and Y offset for every combination of xWind and yWind, as such:
    Java Code:
    if(xWind < 0.50) {
    	if(yWind < 0.50) {
    		offsetX = 1;
    		offsetY = 14;
    	} else if(yWind < 0.70) {
    		offsetX = 1;
    		offsetY = 16;
    	} else if(yWind < 0.80) {
    		offsetX = 1;
    		offsetY = 17;
    	} else if(yWind < 1.00) {
    		offsetX = 2;
    		offsetY = 18;
    	} else {
    		offsetX = 2;
    		offsetY = 19;
    	}
    } else if(xWind < 0.70) {
    	if(yWind < 0.50) {
    		offsetX = 1;
    		offsetY = 14;
    	} else if(yWind < 0.70) {
    		offsetX = 2;
    		offsetY = 17;
    	} 
    	// and so on...
    But that results in 100s of if-else-statements. Is there a better way to do this? I need to be able to manually adjust the offsets for any range of xWind and yWind easily in my code.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    The best way is to use a mathematical formula for calcuating x and y offsets based on the input. How that's done, I have no idea based on what you've told us. Perhaps you can tell us how these numbers are calculated?

  3. #3
    doejohn is offline Member
    Join Date
    Jun 2010
    Posts
    4
    Rep Power
    0

    Default

    Ok thanks. I think the formula is hard to figure out manually, so I guess I'll have to collect as much data as possible so I can use a curve fitting tool to find polynomials that match the data (non-linear regression I guess).

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,530
    Blog Entries
    7
    Rep Power
    20

    Default

    You can do without that long sequence of nested if-else statements but you have to supply the actual data in a couple of matrixes (or, as Fubarable suggested: find a functional depenceny fx(x, y) and fy(x, y) where both fx and fy are the wanted offsets).

    Else a few data matrixes and a bit of code are a solution:

    Java Code:
    double winds[][] = // you have to supply the data here.
    double[] windx[] = // you have to supply the data here.
    int offsetsX[][] = // again, you supply the data here.
    int offsetsY[][] = // again, you supply the data here.
    
    for (int x= 0; x < windx.length; x++)
       if (windx[x] >= actualX)
          for (int y= 0; y < winds[x].length; y++)
             if (winds[x][y] >= actualY)
                // found a value for actualX and actualY: offsetsX[x][y] and offsetsY[x][y] are the answer ...
    kind regards,

    Jos
    Last edited by JosAH; 07-01-2010 at 08:42 PM.

Similar Threads

  1. avoiding memory over-consuming
    By itaipee in forum New To Java
    Replies: 4
    Last Post: 12-14-2009, 11:59 AM
  2. Avoiding system.exit()
    By swati.jyoti in forum New To Java
    Replies: 5
    Last Post: 07-01-2009, 10:17 AM
  3. Avoiding refresh
    By java_srinivasan in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 06-25-2008, 09:01 AM
  4. avoiding if statements
    By valoyivd in forum New To Java
    Replies: 1
    Last Post: 04-02-2008, 09:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •