Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2011
    Posts
    2
    Rep Power
    0

    Default Help with arrays tutorial program please!

    Sup!

    For my course i have to write a java program that will make you enter ten integers and it will then print out the smallest integer! The problem im having is that it works every time except for when i enter "10,9,8,7,6,5,4,3,2,1" And it will say that 5 is the smallest integer! Please can you guys help me out as to where im going wrong! Heres my code! Thanks!

    import java.util.Scanner;

    public class smallestValue
    {
    public static void main(String args[])
    {
    Scanner myKeyboard = new Scanner(System.in);
    int[] numbers = new int[10];
    System.out.println("Enter 10 numbers!");

    for(int i =0;i<numbers.length;i++)
    {
    numbers[i] = myKeyboard.nextInt();
    }

    int smallestNumber = numbers[9];

    for(int i =0;i<numbers.length;i++)
    {
    if(i<smallestNumber)
    {
    smallestNumber = numbers[i];
    }
    }

    System.out.println("Smallest Number: "+smallestNumber);



    }
    }

  2. #2
    kevinn205 is offline Member
    Join Date
    Nov 2011
    Posts
    65
    Rep Power
    0

    Default Re: Help with arrays tutorial program please!

    Perhaps this will make your life easier

    System.out.println(Math.min(26.7, 52.4));

    they bulit-in many math features

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: Help with arrays tutorial program please!

    Step through the program - write down on a piece of paper if you have to - and evaluate how the program flows, what the values are, etc....add some System.out.println statements in there so you can directly evaluate the value of a variable (hint: i != numbers[i] )

  4. #4
    Join Date
    Dec 2011
    Posts
    2
    Rep Power
    0

    Default Re: Help with arrays tutorial program please!

    Thanks! I'll give it another try!

Similar Threads

  1. identical arrays program
    By grcarr77 in forum New To Java
    Replies: 2
    Last Post: 11-10-2011, 10:05 PM
  2. Replies: 2
    Last Post: 10-22-2011, 03:29 PM
  3. Help with Battleship program! 2D arrays
    By tylerkung in forum New To Java
    Replies: 3
    Last Post: 09-23-2011, 01:45 PM
  4. Replies: 1
    Last Post: 03-11-2010, 12:49 AM
  5. Need help with program: arrays
    By dangerzone9k in forum New To Java
    Replies: 2
    Last Post: 04-02-2009, 05:59 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •