for loops and min and max values
I am writing a program that takes a group of numbers input from the keyboard and outputting them with tags to show which is largest and which is smallest. I have an idea of how this is supposed to work. Could any one give me help on how to make a for loop work with this?
I cannot get mine to work at all. I keep getting this really weird number every time I try to run the program. Thanks!!
[404 byte] By [
kita09342a] at [2007-10-2 17:27:43]

Please post your code using [code] and [/code] tags. Post the incorrect results as well.
Hint :- Populate the input numbers into an appropriate collections class and then 'sort' them .. thus you get an ordered list on which you further attach tags while showing the output.Cheers - JiNN
int max;
int min;
int input;
int input2;
int input3;
max = Integer.MIN_VALUE;
min = Integer.MAX_VALUE;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter 3 numbers.");
input = keyboard.nextInt();
input2 = keyboard.nextInt();
input3 = keyboard.nextInt();
if (input < min)
input = min;
else if (input2 < min)
input2 = min;
else if (input3 < min)
input3 = min;
else
min = 0;
if (input > max)
input = max;
else if (input2 > max)
input2 = max;
else if (input3 > max)
input3 = max;
else
max = 0;
System.out.println("The current min number is: " + min);
System.out.println("The current max number is: " + max);
This is what I had. It doesn't work. I don't understand how for loops are supposed to work. I read about them but I still don't understand them.
Ok, let me rephrase that:Please post your code using [code] and[/code] tags (see: http://forum.java.sun.com/help.jspa?sec=formatting). Post the incorrect results as well.
This might also be of help: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/for.html
int max;
int min;
int input;
int input2;
int input3;
max = Integer.MIN_VALUE;
min = Integer.MAX_VALUE;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter 3 numbers.");
input = keyboard.nextInt();
input2 = keyboard.nextInt();
input3 = keyboard.nextInt();
if (input < min)
input = min;
else if (input2 < min)
input2 = min;
else if (input3 < min)
input3 = min;
else
min = 0;
if (input > max)
input = max;
else if (input2 > max)
input2 = max;
else if (input3 > max)
input3 = max;
else
max = 0;
System.out.println("The current min number is: " + min);
System.out.println("The current max number is: " + max);
There should be sequence ofif statements like this
if (input < min){
min = input;
}
if (input2 < min){
min = input2;
}
if (input3 < min){
min = input3;
}
instead of
if (input < min)
input = min;
else if (input2 < min)
input2 = min;
else if (input3 < min)
input3 = min;
else
min = 0;
You still haven't told what it is that's going wrong!Also check the link I posted in reply #5. After reading that, do you understand the for-statement? Try the code example's from there, and implement it in your code.
> You still haven't told what it is that's going
> wrong!
The wrong was that min variable was not assigned.
> Also check the link I posted in reply #5. After
> reading that, do you understand the for-statement?
> Try the code example's from there, and implement it
> in your code.
Is it appropriate implementation of for-statement?
int NumCount = 3;
for( int i = 0; i < NumCount; i++ ){
int input = keyboard.nextInt();
if (input < min){
min = input;
}
}
When I run the code that I just gave you, this is the results that I am getting:The current min number is: 2147483647The current max number is: -2147483648I have no idea where those numbers are coming from!!!! NO COMPRENDE!!
> > You still haven't told what it is that's going> > wrong!> > The wrong was that min variable was not assigned.No, I meant the OP didn't tell what it is that's going wrong.; )
I will try to read the page you gave me and more in my text book to see if I can understand how this is supposed to work. I am still not getting it but thanks for the help...
> The current min number is: 2147483647
> The current max number is: -2147483648
>
> I have no idea where those numbers are coming
> from!!!!
Those are the initial values: Integer.MAX_VALUE and Integer.MIN_VALUE
> NO COMPRENDE!!
Read raindrop's last reply, there's an example how to use the for-statement and how to check an input number.
> I will try to read the page you gave me and more in> my text book to see if I can understand how this is> supposed to work. I am still not getting it but> thanks for the help...REPLY 9 !!!