Page 1 of 2 12 LastLast
Results 1 to 20 of 24
  1. #1
    Cherno is offline Member
    Join Date
    Nov 2011
    Posts
    12
    Rep Power
    0

    Default If A Value of a Variable is increasing...

    Hi!

    I was wondering how I can make an if statement for if a value of a variable is increasing.
    For example:

    if (x++) {
    dothis();
    }

    That of course gives me a syntax error. How can I make something like this?

    Thanks for any help!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: If A Value of a Variable is increasing...

    if a value of a variable is increasing.
    To detect if the value of a variable has been increased, you need to have saved the old value of the variable to compare against.

  3. #3
    Cherno is offline Member
    Join Date
    Nov 2011
    Posts
    12
    Rep Power
    0

    Default Re: If A Value of a Variable is increasing...

    And how do I do that? ;)

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: If A Value of a Variable is increasing...

    Create a new variable and assign it the value of the variable you want to test for changes.

  5. #5
    Cherno is offline Member
    Join Date
    Nov 2011
    Posts
    12
    Rep Power
    0

    Default Re: If A Value of a Variable is increasing...

    But how can I put this into an if statement?

    Let me tell you what I want to do. I've got an input handler class which implements a MouseMotionListener.
    public void mouseMoved(MouseEvent e) {
    mouseX = e.getX();
    mouseY = e.getY();
    }

    Now I want to basically say:

    if (mouseX++) {
    variable++;
    }

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: If A Value of a Variable is increasing...

    Save the old value of mouseX in a new variable and then test it against the new value of mouseX.
    You need to think through the logic for when the value of mouseX is set and when to save it so you can detect if it has changed and when you want to test it.

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default Re: If A Value of a Variable is increasing...

    As Norm said you need to have the old value stored.
    Java Code:
    if(newValue > oldValue) {
        it has increased
    }

  8. #8
    Cherno is offline Member
    Join Date
    Nov 2011
    Posts
    12
    Rep Power
    0

    Default Re: If A Value of a Variable is increasing...

    Yeah, but the old value of x is EQUAL to the new value of x. They increase at the same time.

    How can I save an OLD version of the X variable? It just changes into the new version straight away, obviously.

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default Re: If A Value of a Variable is increasing...

    Java Code:
    import java.util.Scanner;
    
    public class Test {
        public static void main(String[] args) {
            Scanner reader = new Scanner(System.in);
            int oldValue = 0;
            int newValue = 0;
            boolean finished = false;
            System.out.println("Enter a series of numbers. A negative number to stop!");
            while(! finished) {
                System.out.print(">> ");
                newValue = reader.nextInt();
                if(newValue < 0) {
                    finished = true;
                } else {
                    if(newValue > oldValue) {
                        System.out.println("The number has increased");
                    } else {
                        System.out.println("The number has decreased");
                    }
                    oldValue = newValue;
                }
            }
        }
    }

  10. #10
    Cherno is offline Member
    Join Date
    Nov 2011
    Posts
    12
    Rep Power
    0

    Default Re: If A Value of a Variable is increasing...

    Thanks for the example, but in my case, oldValue is already EQUAL to newValue. If I move the mouse horizontally to the right then it increases the x variable's value, and if I move it to the left it decreases that value.
    So how can I check if the value has been increased, decreased, or doesn't change?

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default Re: If A Value of a Variable is increasing...

    :headdesk:

    When you move the mouse ONLY the new value will change. You make the comparison against the old value and after that you reset old value to new value. Exactly how I did in my example.

  12. #12
    Cherno is offline Member
    Join Date
    Nov 2011
    Posts
    12
    Rep Power
    0

    Default Re: If A Value of a Variable is increasing...

    From what I understand, what we're doing here is sort of setting like an origin point somewhere on the screen, and then using that to measure whether the mouse value increases or decreases.

    But what I want here is for that point of origin to sort of follow the mouse around, so that if I move the mouse 500 pixels to the right, and then 1 pixel to the left, it will measure that 1 pixel to the left as a decrease, and those 500 pixels to the right as an increase. I'm sorry if this is hard to understand, it is honestly hard to explain!

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default Re: If A Value of a Variable is increasing...

    No it is not hard to understand FOR US!

    each time the mouse moves
    get current x-coord
    compare with last x-coord (stored in a different variable)
    log change
    update last x-coord to equal current x-coord

  14. #14
    Cherno is offline Member
    Join Date
    Nov 2011
    Posts
    12
    Rep Power
    0

    Default Re: If A Value of a Variable is increasing...

    Yeah that's pretty much what I want, but how do I store the LAST x-coord? Could you give some example code?

    Current x-coord = e.getMouseX();
    but how can I log what that coord was say, 1 ms ago?

  15. #15
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default Re: If A Value of a Variable is increasing...

    If you are doing this inside the mouseMoved method then there is no concept of time. The only thing that matters is that the co-ords have changed. Surely I have provided enough hints as well as code for you to figure this out by now.

  16. #16
    Cherno is offline Member
    Join Date
    Nov 2011
    Posts
    12
    Rep Power
    0

    Default Re: If A Value of a Variable is increasing...

    Thanks a lot! It worked! Took me a while to work it out and adjust it to the MouseMotionListener. Turns out it didn't work while in the the mouseMoved method, and had to be moved out of there; the mouse coordinates wouldn't update while in there.

    One other thing I was wondering about, how do I make my mouse loop the window? So when I get to the right edge of the frame, the mouse appears at the left edge, and vice versa?

  17. #17
    Cherno is offline Member
    Join Date
    Nov 2011
    Posts
    12
    Rep Power
    0

    Default Re: If A Value of a Variable is increasing...

    And also while we're on that note, is there any where to measure the speed at which the mouse moves? If so, how?

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: If A Value of a Variable is increasing...

    any where to measure the speed at which the mouse moves
    Save the current time (See System.currentTimeMillis()) with the current mouse location. Then compute the distance moved divided by the time to move it.

  19. #19
    Cherno is offline Member
    Join Date
    Nov 2011
    Posts
    12
    Rep Power
    0

    Default Re: If A Value of a Variable is increasing...

    But how can I save time tied to the mouse location integer?

    And also how do I loop the mouse when it exits the frame? So when I get to the right edge of the frame, the mouse appears at the left edge, and vice versa?

    Sorry I'm asking so many questions, but you guys are awesome for helping me out!

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: If A Value of a Variable is increasing...

    When you save the mouse location, save the current time also.
    Then when you get a new mouse location, get the current time.
    Now you have the change in location and the change in time that you can use to compute the speed.
    how do I loop the mouse when it exits the frame? So when I get to the right edge of the frame, the mouse appears at the left edge,
    You need to detect when you are at the edge of the frame and change the location to be at the other edge.
    Are you planning to use the Robot class to change the location of the cursor?

Page 1 of 2 12 LastLast

Similar Threads

  1. Colour increasing and retaining animation
    By monica in forum Java 2D
    Replies: 7
    Last Post: 05-15-2011, 04:56 PM
  2. Increasing/decreasing an int variable with boundrys
    By david522 in forum New To Java
    Replies: 5
    Last Post: 05-12-2011, 12:04 AM
  3. Increasing Java heap space
    By davetheant in forum New To Java
    Replies: 2
    Last Post: 02-03-2011, 02:24 AM
  4. increasing jvm heap space
    By kizyle502 in forum Advanced Java
    Replies: 1
    Last Post: 05-15-2010, 10:51 AM
  5. all maximal increasing subsequences
    By hassanJava in forum Advanced Java
    Replies: 3
    Last Post: 04-23-2008, 04:05 PM

Tags for this Thread

Posting Permissions

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