Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: previous value of same variable

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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.
    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);           
        }
    }

    regard
    ray


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: previous value of same variable

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/63128-previous-value-same-variable.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: previous value of same variable

    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

  4. The Following User Says Thank You to helloworld922 For This Useful Post:

    raybrown (September 19th, 2012)

  5. #4
    Junior Member
    Join Date
    Sep 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Talking Re: previous value of same variable

    Quote Originally Posted by helloworld922 View Post
    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
    Perfect work-flow.
    I understand and can do it.
    Thank you very much for your help.

    ray

Similar Threads

  1. Previous Search history for JtextField in swings
    By kumaruri in forum AWT / Java Swing
    Replies: 2
    Last Post: April 18th, 2012, 08:37 PM
  2. Replies: 5
    Last Post: November 16th, 2011, 11:22 AM
  3. MySQL reading, next en previous button
    By superdave in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 29th, 2011, 09:26 AM
  4. Need to uninstall the previous version of java?
    By akanksha4000 in forum Java Theory & Questions
    Replies: 1
    Last Post: March 25th, 2011, 09:40 AM
  5. new arrayList element overwrites all previous
    By twinkletoes in forum Object Oriented Programming
    Replies: 2
    Last Post: April 2nd, 2010, 07:45 AM