-
Help - 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: Help - Loops
See my response in java.getting.started.
PS. cross-posting is not encouraged on this site (in fact, it is discouraged)...

"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: Help - Loops
"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
try to use else if { it is more powerfull than using the esle on it own
}
public void setMaxValue(int value)
{
if (value > maxValue)
{
maxValue = value;
}
else if ( value < maxvalue )
{
maxValue = maxValue;
}
}
-
Re: Help - Loops
Hi Andy,
Please try as shown, hope it helps.
Madhu.
--------------------------------
public class Test
{
int maxValue = 100;
public void maxValue(int value)
{
if (value > maxValue)
{ maxValue = value;
}
System.out.println("now = " + maxValue);
}
public static void main(String[] args)
{
Test t = new Test();
t.maxValue(50);
t.maxValue(200);
}
}
-
Re: Help - Loops
Hi Andy,
Please try as shown, hope it helps.
Madhu.
--------------------------------
public class Test
{
int maxValue = 100;
public void maxValue(int value)
{
if (value > maxValue)
{ maxValue = value;
}
System.out.println("now = " + maxValue);
}
public static void main(String[] args)
{
Test t = new Test();
t.maxValue(50);
t.maxValue(200);
}
}
-
Re: Help - Loops
Hi Andy
Your original code is correct , so it must be how you are using it.
I've included a full class below , have a look at it , the output
is in the comment after the class.
BTW the if ... else is not a loop , its a branch , loops are things
like for , while ... which run through their bit of code usualy more
that once , i.e. "loop" round a section of code. Also the advice you
where given about if ... else if ... would not be right in your case
as you just want to either do one thing or another depending on the
result of the test , an else if ( secondTest ) introduces a second ,
different test if the first one fails. It would however be correct
for different types of tests , e.g. If the code had to different things
depending on several ranges ( 0-100 , 101-200 , 201-500 ... ).
public class MaxNumber
{
int maxValue = 0;
public MaxNumber( int seed )
{
maxValue = seed;
}
public void setMaxValue(int value)
{
if (value > maxValue)
{
maxValue = value;
}
else
{
maxValue = maxValue;
}
}
public void showMax()
{
System.out.println( "Current Max is : " + maxValue );
}
public static void main ( String clargs[] )
{
MaxNumber testit = new MaxNumber( 20 );
testit.setMaxValue( 10 );
testit.showMax();
testit.setMaxValue( 20 );
testit.showMax();
testit.setMaxValue( 21 );
testit.showMax();
testit.setMaxValue( 30 );
testit.showMax();
}
}
/******** Output *****************
E:\m301\Temp>java MaxNumber
Current Max is : 20
Current Max is : 20
Current Max is : 21
Current Max is : 30
********* Output *****************/
Bye David
"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
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
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks