need program that finds smallest of several prompted integers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dfluker
    New Member
    • Feb 2010
    • 10

    need program that finds smallest of several prompted integers

    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?
  • dfluker
    New Member
    • Feb 2010
    • 10

    #2
    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 );
    }
    }

    Comment

    • pbrockway2
      Recognized Expert New Member
      • Nov 2007
      • 151

      #3
      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

      Working...