Results 1 to 4 of 4
- 12-12-2011, 09:31 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 2
- Rep Power
- 0
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);
}
}
- 12-12-2011, 09:44 PM #2
Member
- Join Date
- Nov 2011
- Posts
- 65
- Rep Power
- 0
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
- 12-12-2011, 09:45 PM #3
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,605
- Rep Power
- 5
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] )
- 12-12-2011, 10:00 PM #4
Member
- Join Date
- Dec 2011
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
identical arrays program
By grcarr77 in forum New To JavaReplies: 2Last Post: 11-10-2011, 10:05 PM -
Just made my first Java program without a tutorial
By Vatec in forum New To JavaReplies: 2Last Post: 10-22-2011, 03:29 PM -
Help with Battleship program! 2D arrays
By tylerkung in forum New To JavaReplies: 3Last Post: 09-23-2011, 01:45 PM -
Please help me with this program (Linear Search two arrays)
By Meta in forum New To JavaReplies: 1Last Post: 03-11-2010, 12:49 AM -
Need help with program: arrays
By dangerzone9k in forum New To JavaReplies: 2Last Post: 04-02-2009, 05:59 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks