import javax.swing.JOptionPane;
public class largestsmallest {
public static void main(String[] args) {
int counter=0;
int number=0;
int largest = number;
int smallest = number;
while (counter<5){
counter++;
number = Integer.parseInt(JOptionPane.showInputDialog("Enter number"));
if(number > largest){largest=number;
JOptionPane.showMessageDialog(null," largest " + largest, "largest", JOptionPane.PLAIN_MESSAGE);
}
else if(number < smallest)smallest=number;{
JOptionPane.showMessageDialog(null," smallest" + smallest, "smallest", JOptionPane.PLAIN_MESSAGE);
}
}
System.exit(0);
} //end main
} //end class
Outputing Smallest and Largest Integer
Started by Kinsleyy, Jan 24 2010 12:41 PM
5 replies to this topic
#1
Posted 24 January 2010 - 12:41 PM
I am trying to write an application that reads 5 integers and then reads and prints the largest and smallest integers in the group. This is what I have.
|
|
|
#2
Posted 24 January 2010 - 01:03 PM
I cannot find where my error is. Any help will help, thanks guys
#3
Posted 24 January 2010 - 01:05 PM
Two things. First, is that the smallest and largest integers, initially, are zero, instead of the largest and smallest possible values. You should have Integer.MAX_VALUE and Integer.MIN_VALUE for the initial values of the smallest and largest.
Also, you've got a bracket out of place here:
Also, you've got a bracket out of place here:
else if(number < smallest)smallest=number;[COLOR=RED]{[/COLOR]
#4
Posted 24 January 2010 - 01:17 PM
I cannot find where the bracket needs to be.
#5
Posted 24 January 2010 - 01:59 PM
right after the if-clause.
Pretty logical, if you ask me...
Pretty logical, if you ask me...
#6
Posted 24 January 2010 - 03:05 PM
Thanks! I have the app working. The bracket was logical too..srry


Sign In
Create Account

Back to top









