Results 1 to 13 of 13
Like Tree1Likes
  • 1 Post By thinkinjava24

Thread: previous value of same variable

  1. #1
    raybrown is offline Member
    Join Date
    Sep 2012
    Posts
    6
    Rep Power
    0

    Default previous value of same variable

    How can I compare previous value of same variable(result) with current value.
    Example, result 1 > result 2 or not, result 2 > result 3 or not, result 3 > result 4 or not, result 4 > result 5 or not.

    Java Code:
    import java.util.Random;
    public class Test {
        public static void main(String args[]){
            double A = 5;
            Test Cal = new Test();
            Cal.run(A);        
        }
        void run(double A){
            Random random = new Random();
            double result;
            int i = 0;
            do{
               i++;
                    result = A + random.nextGaussian() / 2;
                    System.out.println(result);
            }while(i<5);           
        }
    }
    Thank you
    Ray

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: previous value of same variable

    You need to store the previous value in another variable.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    raybrown is offline Member
    Join Date
    Sep 2012
    Posts
    6
    Rep Power
    0

    Default Re: previous value of same variable

    Quote Originally Posted by Tolls View Post
    You need to store the previous value in another variable.
    Can you give me some code.

    regard
    ray

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: previous value of same variable

    See my sig.

    My post should have given you enough to at least give it a go.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    raybrown is offline Member
    Join Date
    Sep 2012
    Posts
    6
    Rep Power
    0

    Default Re: previous value of same variable

    oh sorry, It's first time for me here.

    regard
    ray

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: previous value of same variable


  7. #7
    raybrown is offline Member
    Join Date
    Sep 2012
    Posts
    6
    Rep Power
    0

    Default Re: previous value of same variable

    I try this it's perfect work-flow.
    Keep track of the previous result in a temporary variable.

    Example work-flow:

    Init. 2 variables, last val and current val.

    Get current val, set last val equal to current val.
    for i = 1, i < 5, ++i
    get a new current val
    compare it to the last val
    set last val equal to current val

    ^__________^
    reference
    previous value of same variable
    Last edited by raybrown; 09-19-2012 at 09:15 PM.

  8. #8
    thinkinjava24 is offline Member
    Join Date
    Sep 2012
    Posts
    17
    Rep Power
    0

    Default Re: previous value of same variable

    import java.util.*;
    public class Test {
    public static void main(String args[]){
    double A = 5;
    Test Cal = new Test();
    Cal.run(A);
    }
    void run(double A){
    Random random = new Random();
    double[] result = new double[5];
    int i = 0;
    do{
    result[i] = A + random.nextGaussian() / 2;
    if(i > 0) {
    if(compare(result[i], result[i-1]))
    System.out.println("the larger one is " + result[i]);
    else
    System.out.println("the larger one is " + result[i-1]);

    }
    i++;
    }while(i<5);
    }

    boolean compare(double i, double j) {
    if(i >= j)
    return true;
    else return false;
    }
    }
    raybrown likes this.

  9. #9
    raybrown is offline Member
    Join Date
    Sep 2012
    Posts
    6
    Rep Power
    0

    Default Re: previous value of same variable

    thinkinjava24, Thank you for your advice.

    regard
    ray

  10. #10
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: previous value of same variable

    Quote Originally Posted by raybrown View Post
    thinkinjava24, Thank you for your advice
    I hope you truly understood the code that was posted. In forum lingo, we call this spoonfeeding, and it is frowned upon - especially when absolutely no explanation was given regarding the code. Often times responses such as this are deleted by the moderating staff because of this. See The Problem with Spoon-feeding
    Last edited by doWhile; 09-20-2012 at 05:57 AM.

  11. #11
    thinkinjava24 is offline Member
    Join Date
    Sep 2012
    Posts
    17
    Rep Power
    0

    Default Re: previous value of same variable

    you 're welcome。 seems that i went against some rules in forum....

  12. #12
    thinkinjava24 is offline Member
    Join Date
    Sep 2012
    Posts
    17
    Rep Power
    0

    Default Re: previous value of same variable

    Quote Originally Posted by doWhile View Post
    I hope you truly understood the code that was posted. In forum lingo, we call this spoonfeeding, and it is frowned upon - especially when absolutely no explanation was given regarding the code. Often times responses such as this are deleted by the moderating staff because of this. See The Problem with Spoon-feeding
    sorry about that.. this is really the first time to offer help. would not happen again...

  13. #13
    raybrown is offline Member
    Join Date
    Sep 2012
    Posts
    6
    Rep Power
    0

    Default Re: previous value of same variable

    sorry too, would not happen again.

    regard
    ray

Similar Threads

  1. Next and Previous Buttons
    By kbgriggs in forum New To Java
    Replies: 2
    Last Post: 08-12-2009, 02:08 PM
  2. how to store the previous of a variable
    By Randa in forum New To Java
    Replies: 7
    Last Post: 09-22-2008, 08:16 PM
  3. add previous jar library to new one
    By Farzaneh in forum Eclipse
    Replies: 1
    Last Post: 08-23-2008, 02:31 PM
  4. help..on Next n Previous Button
    By norazanita in forum New To Java
    Replies: 0
    Last Post: 06-12-2008, 05:58 AM
  5. Using previous with CardLayout
    By uncopywritable in forum New To Java
    Replies: 2
    Last Post: 08-05-2007, 09:43 PM

Posting Permissions

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