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: If Statements help

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    10
    My Mood
    Amused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default If Statements help

    I need help with some homework code. I was asked to increment and decrement the temperature based on the increment field predifined by the user. The temperature is set to 50 by default. So, my problem is that if i set the maximum temperature to 85 and increment by 25. Once I run the warmer method below, and the temperature gets incremented to 100 (even though my IF statement is < maximum) and then it displays the error message once I run the warmer method again. I don't know why my if statement adds the value of the increment field and then displays the error message. I working with blueJ IDE.


     
    public int warmer()
        {
     
            if (temperature < maximum)
     
                {
                    temperature= increment + temperature;
                    //return temperature;
     
                } 
            else 
                {
                    System.out.println("You cannot increase above the predifined maximum temperature \n Please set maximum > " + maximum);
     
                }   
     
                    return temperature;
     
        }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: If Statements help

    Do you want to test if the current value plus the increment will exceed the max value BEFORE adding the increment?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2012
    Posts
    10
    My Mood
    Amused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Norm View Post
    Do you want to test if the current value plus the increment will exceed the max value BEFORE adding the increment?
    Yeah kind of like that

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: If Statements help

    What would be the condition in the if to test for that?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Using JOptionPane with if-else statements
    By historymaiden in forum AWT / Java Swing
    Replies: 1
    Last Post: July 10th, 2012, 10:59 AM
  2. Need help with chars/or's/if statements
    By czarcalvinsk in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 4th, 2012, 08:46 PM
  3. if else statements
    By mozyman in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 24th, 2010, 08:06 PM
  4. If statements
    By Scottj996 in forum Java Theory & Questions
    Replies: 1
    Last Post: August 16th, 2010, 10:09 AM
  5. Need help with While and For Statements
    By duckman in forum Loops & Control Statements
    Replies: 2
    Last Post: October 20th, 2009, 08:42 PM