Re: compare biggest negative number
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
w00 schreef:
Hello, i have to write a program in java where i have to enter three
different numbers, and it will output the largest number. So for
example:
input: 2, 4 ,6 => output: 7
input: 5, 2 ,1 => output: 5
input: 7, 9 ,3 => output: 9
My program works well, but not with negative values, cause it always
returns a 0.
So if i would enter the following:
input: -2, -4 -,6
Then the output should be -2, but i get a '0' instead. Any idea why
this is happening and what i can do to fix it?
Here's my code:
import javax.swing.*;
public class test
{
public static void main(String args[])
{
String input;
int intNum;
int prevNum=0;
Here you initiate the previous number to 0.
int output=0;
for(int i=0; i<3; i++)
{
<snip>
if(prevNum < intNum)
{
prevNum = intNum;
}
And here you set the previous number to the current number if it is
smaller. Now think what would happen if all intNums are negative.
<snip>
Maybe you should choose a better number to initiate prevNum with. (Some
static number in Integer comes to mind.)
HTH, H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
iD8DBQFFZBIoe+7xMGD3itQRAhQIAJ43vgHJAZanbTzeLry2klJvNIb6SACfZl+y
3yZnYPBFhDCWlkw/Ov7K730=
=cLTk
-----END PGP SIGNATURE-----