Results 1 to 6 of 6
  1. #1
    david522 is offline Member
    Join Date
    May 2011
    Posts
    21
    Rep Power
    0

    Default Increasing/decreasing an int variable with boundrys

    I am defining two methods where I am increasing a variable by 1 at a time and decreasing by 1 at a time.

    public void moveUp() {
    currentScore = currentScore + 1;
    }

    I also have an int value which represents the maximum score and thus the score can not go over this. How could I implement code such that nothing will happen if the user choses to increase their score but their score is already at the maximum?

    Also for decreasing I need to make sure that when the score is 0, it can not be decreased furthur.

    Thanks.

  2. #2
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Java Code:
    public void moveUp() {
    if (currentScore != maximum)
    currentScore = currentScore + 1;
    else
    System.out.println("Score is at its maximum!");
    }

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

    Default

    Quote Originally Posted by david522 View Post
    How could I implement code such that nothing will happen IF the user choses to increase their score but their score is already at the maximum?
    You knew the answer to your own question!

  4. #4
    david522 is offline Member
    Join Date
    May 2011
    Posts
    21
    Rep Power
    0

    Default

    Java Code:
    private int initNumScores;
    	public B (int initNumScores) {
    	this.initNumScores = initNumScores;
    	
    	}
    	public void moveUp() {
    		if(initNumScores != currentScore) {
    	     currentScore = currentscore + 1;
    		} else {
    			this.currentScore = currentScore;
    		}
    		
    	}
    This is just part of my class (B), but basically the moveup method is called using something which is created by the constructor method that I have written. However the score continues to increase... I have 3 class A, B ,C. Not sure if I need to provide you guys with any more code. Sorry I'm not very clear..

  5. #5
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by david522 View Post
    Java Code:
    private int initNumScores;
    	public B (int initNumScores) {
    	this.initNumScores = initNumScores;
    	
    	}
    	public void moveUp() {
    		if(initNumScores != currentScore) {
    	     currentScore = currentscore + 1;
    		} else {
    			this.currentScore = currentScore;
    		}
    		
    	}
    This is just part of my class (B), but basically the moveup method is called using something which is created by the constructor method that I have written. However the score continues to increase... I have 3 class A, B ,C. Not sure if I need to provide you guys with any more code. Sorry I'm not very clear..
    You completely ignored my post.

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

    Default

    Java Code:
    class Test {
        private int MAX = 5;
        private int current = 0;
    
        private void inc() {
            if(current < MAX) {
                current++;
                System.out.println(current);
            } else {
                System.out.println("MAX has been reached");
            }
        }
    
        public void run() {
            inc();
            inc();
            inc();
            inc();
            inc();
            inc();
        }
    
        public static void main(String[] args) {
            new Test().run();
        }
    }
    It works fine for me. You must be doing something wrong elsewhere in your code.

    Java Code:
    this.currentScore = currentScore;
    By the way that is pointless.

Similar Threads

  1. Suggestions needed for decreasing memory usage
    By TheEnemy in forum Advanced Java
    Replies: 1
    Last Post: 02-14-2011, 05:12 AM
  2. Increasing Java heap space
    By davetheant in forum New To Java
    Replies: 2
    Last Post: 02-03-2011, 02:24 AM
  3. Replies: 9
    Last Post: 01-24-2011, 12:17 PM
  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

Posting Permissions

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