Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 750555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:33:52+00:00 2026-05-14T14:33:52+00:00

I would like to go from one number to another. For example if I

  • 0

I would like to go from one number to another. For example if I started at 6 and my goal was 10 I want a function that on every pass would bring me from 6 towards 10 or if I had the number 14 and my goal was 9 it would count down from 14 towards 9.So far I have (this is written in Processing a Java Api but there is essentially no difference from regualr Java, draw is just a continuous loop)

int x=100;
void draw(){
  x=towards(x,10);
  println(x);

}

int towards(int current ,int target){
  if(current!=target){
    if (current <target){
      current=current+1;
    }
    else {
      current=current-1;
    }
  }
  return current;
}

this gives me the results I would like but I would like to have everything in side of the towards() function. When I replace X with a variable it of course resets it self to the static variable.

To sum it up how can I pass a variable to a function and have that variable thats been passed change on every subsequent pass. I have looked into recursion as a solution but that of just brings me to a final solution. I can pass the count to an array but wouldn’t like to do that either.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T14:33:52+00:00Added an answer on May 14, 2026 at 2:33 pm

    Java uses pass-by-value, and so you cannot change the parameters (although if the parameters is a user-defined object, you can change the object pointed-to by the parameter, but not the parameter, itself).

    One thing you might consider, though, is to create an iterator type interface:

    // RangeIterator.java
    public class RangeIterator implements Iterator<Integer>
    {
       public RangeIterator(int first, int last){
           _first = first;
           _last = last;
           if ( _first <= _last ){
               _step = 1;
           }else{
               _step = -1;
           }
       }
    
       public RangeIterator(int first, int last, int step){
           if ( step == 0 ){
                throw new IllegalArgumentException("Step must be non-zero.");
           }
           _first = first;
           _last = last;
           _step = step;
       }
    
       public boolean hasNext(){
           if ( _step < 0 ){
               return _first > _last;
           } else {
               return _first < _last;
           }
       }
    
       public Integer next(){
           int result = _first;
           _first += _step;
           return result;
       }
    
       public void remove(){
           throw new UnsupportedOperationException("Not implemented.");
       }
    
       private int _first;
       private int _last;
       private int _step;
    }
    
    // Range.java
    public class Range implements Iterable<Integer>
    {
         public Range(int first, int last){
           _first = first;
           _last = last;
           if ( _first <= _last ){
               _step = 1;
           }else{
               _step = -1;
           }
         }
    
         public Range(int first, int last, int step){
           if ( step == 0 ){
                throw new IllegalArgumentException("Step must be non-zero.");
           }
           _first = first;
           _last = last;
           _step = step;
         }
    
         public Iterator<Integer> iterator(){ 
               return new RangeIterator(_first,_last,_step); 
         }
    
         private int _first;
         private int _last;
         private int _step;
    }
    

    With the code above, you could then conveniently write something like:

    Range range = new Range(x,100);
    for (int val : range){
        println(val);
    }
    

    Since I see that you come from a Python background, this should feel very much like:

    for val in xrange(x,100):
        print val;
    

    You can implement the Iterable and Iterator interfaces in order to provide your own generators that can be used in the Java for-each loops. Basically, for (Type identifier1 : identifier2) iterates through the contents of identifier2 which needs to be an Iterable type, and on each iteration assigns the current element to identifier1. The loop is just syntactic sugar for iterating over identifier2.iterator().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to change all captions from one type to another. While not
I would like to store the cookies from one open-uri call and pass them
I would like to copy a macro from one Excel workbook to another using
I would like to subtract one value from another value. The schema of the
I have a request in from a client that would like one of their
I am using jquery mobile. I would like to navigate from one page to
I would like to know how one goes about adding functionality from one open
I would like to combine these four queries into one: SELECT COUNT(ID) FROM conversations
I have a simple one-to-many relationship. I would like to select rows from the
I'd like to replace one string from one file with a string from another

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.