Loops


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Loops

  1. #1
    Andy Guest

    Loops


    Can anyone help!

    I am trying to use a if else loop and it does not seem to work.
    What I am trying to do is create a class called maxNumber with all methods
    in for a test class which calls the maxNumber class to return the values.The
    code that I am having problems with is
    below.

    //Updates the maximum value to the value specified
    //if necessary - if the value is greater than the
    //current maxValue.

    public void setMaxValue(int value)
    {
    if (value > maxValue)
    {
    maxValue = value;
    }
    else
    {
    maxValue = maxValue;
    }
    }
    I want the maxValue to be updated by the value in the parameter only if the
    value is above the current maxValue.
    Help!!

    Andy

  2. #2
    Kent Guest

    Re: Loops


    Andy,

    I see no reason why this code wouldn't work assuming you have a declaration
    such as:

    private int maxValue = 0;

    in your class. The else clause in your if statement is pointless:

    maxValue = maxValue

    achieves absolutely nothing but wasting time so I suggest you remove that.

    Kent


    "Andy" <andrew.constable@ntlworld.com> wrote:
    >
    >Can anyone help!
    >
    >I am trying to use a if else loop and it does not seem to work.
    >What I am trying to do is create a class called maxNumber with all methods
    >in for a test class which calls the maxNumber class to return the values.The
    >code that I am having problems with is
    >below.
    >
    > //Updates the maximum value to the value specified
    > //if necessary - if the value is greater than the
    > //current maxValue.
    >
    > public void setMaxValue(int value)
    > {
    > if (value > maxValue)
    > {
    > maxValue = value;
    > }
    > else
    > {
    > maxValue = maxValue;
    > }
    > }
    >I want the maxValue to be updated by the value in the parameter only if

    the
    >value is above the current maxValue.
    >Help!!
    >
    >Andy



  3. #3
    Guest

    Re: Loops

    another method to do this:

    maxValue = (value > maxValue) ? value : maxValue;


    "Andy" <andrew.constable@ntlworld.com> wrote in message
    news:3a833e19$1@news.devx.com...
    >
    > Can anyone help!
    >
    > I am trying to use a if else loop and it does not seem to work.
    > What I am trying to do is create a class called maxNumber with all methods
    > in for a test class which calls the maxNumber class to return the

    values.The
    > code that I am having problems with is
    > below.
    >
    > //Updates the maximum value to the value specified
    > //if necessary - if the value is greater than the
    > //current maxValue.
    >
    > public void setMaxValue(int value)
    > {
    > if (value > maxValue)
    > {
    > maxValue = value;
    > }
    > else
    > {
    > maxValue = maxValue;
    > }
    > }
    > I want the maxValue to be updated by the value in the parameter only if

    the
    > value is above the current maxValue.
    > Help!!
    >
    > Andy




  4. #4
    Paul Clapham Guest

    Re: Loops

    And another way: maxValue = Math.max(maxValue, value);

    <anthony> wrote in message news:3a84c364@news.devx.com...
    > another method to do this:
    >
    > maxValue = (value > maxValue) ? value : maxValue;
    >





Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center