I need to write a java application that finds the smallest of several integers. I need it to prompt the user for the number of integers that will be input, then prompt the user for that number of integers. It's suppose to evaluate the integers to determine the smallest value . . . I know this is easy but all i'm getting is an assortment from least to greatest numbers... can someone help?
need program that finds smallest of several prompted integers
Collapse
X
-
i figured it out
I figured it out/
// Small.java
// Program finds the smallest of several letters
import javax.swing.JOp tionPane;
public class Small {
public static void main( String args[] )
{
int smallest = 0, temp = 0, number;
String input;
input = JOptionPane.sho wInputDialog(
"Enter number of integers:" );
number = Integer.parseIn t( input );
if ( number == 0 )
System.exit( 0 );
for ( int x = 1; x <= number; x++ ) {
input = JOptionPane.sho wInputDialog(
"Enter integer:" );
temp = Integer.parseIn t( input );
if ( x == 1 )
smallest = temp;
else if ( temp < smallest )
smallest = temp;
}
JOptionPane.sho wMessageDialog(
null, "Smallest Integer is: " + smallest,
"Result", JOptionPane.INF ORMATION_MESSAG E );
System.exit( 0 );
}
} -
Good work!
Most people would write the for loop as (int x = 0; x < number; x++), I think. It's never to early to embrace zero-based counting! Also you really should use braces even for single line if and else blocks: I guarantee it will save you grief later.
(I'm guessing that your posting of the code was an invitation for comments ... ;)Comment
Comment