Hi Praveen
The condition "counter == 0" grants an initial value into the "max"
variable. Also, the initial "zero" value is assumed.
In order to see what I mean, imagine that all the numbers you enter are
negatives (less than zero). With the condition "if ( counter == 0 ||
..." the program will find the actual maximum. If you skip the
condition, it will find that the maximum is zero (although all the
entered numbers are less then zero).
Hope it helps
Mihai
Le 17/11/2010 19:47, Praveen D'Souza a écrit :
import javax.swing.JOptionPane;
public class GreatestNumber {
public static void main(String[] args) {
int[] num = new int[10];
int counter;
int max = 0;
int totalnumber = 3;
// Prompt a user to enter numbers
for(counter = 0; counter< totalnumber; counter++){
num[counter] = Integer.parseInt
(JOptionPane.showInputDialog("Enter numbers until
" + totalnumber + " numbers are entered"));
// Compute the greatest number up to this point
if((counter == 0)||(num[counter]> max))
max = num[counter];
}
// Display the greatest number.
JOptionPane.showMessageDialog(null,"The number with the
greatest value is " + max);
}
}
from if((counter == 0)||(num[counter]> max)) if i remove
counter==0 it still finds the max..what is the purpose of having
counter ==0????
--
To post to this group, send email to javaprogrammingwithpassion@googlegroups.com
To unsubscribe from this group, send email to
javaprogrammingwithpassion+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en