-
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
-
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
-
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
-
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
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|