Results 1 to 2 of 2
  1. #1
    kris09 is offline Member
    Join Date
    Aug 2008
    Rep Power

    Default Least To Greates[ if statement and for loop]

    Here is the program that i wrote what i want to do is order numbers from least to greatest with some if statements and print statements with the for loop what i cant figure out is the logic for the if statement. I can only compare 2 numbers not 5 HOW DO I DO THAT?? thanks for the help guys

    import java.util.Scanner;

    public class LeastToGreatestArray {

    public static void main(String []args) {
    Scanner myScanner = new Scanner(;
    int [] myArray;
    int num1, num2, num3, num4, num5;
    myArray = new int [5];

    System.out.print("First Number: ");
    num1 = myScanner.nextInt();
    System.out.print("Second Number: ");
    num2 = myScanner.nextInt();
    System.out.print("Third Number: ");
    num3 = myScanner.nextInt();
    System.out.print("Fourth Number: ");
    num4 = myScanner.nextInt();
    System.out.print("Fifth Number: ");
    num5 = myScanner.nextInt();

    myArray[0] = num1;
    myArray[1] = num2;
    myArray[2] = num3;
    myArray[3] = num4;
    myArray[4] = num5;
    //System.out.print(MyArray[0]); check if it works
    //System.out.print(MyArray[1]); check !!

    if(myArray[0] < myArray[1]){
    System.out.print(myArray[0] + "is less than" + myArray[1]);


  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power


    can only compare 2 numbers
    That's what arrays allow you to do. If you put all the numbers in an array, you can look at each one in turn by varying the index to the array. You can use multiple indexes to point to different parts of the array. Use a for() loop to move thru the contents of the array.
    To visualize the problem use a piece of paper, make a list of a couple of numbers, set pointers(indexes) to two numbers, compare them and then move the pointers and/or the numbers around until you get the algorithm.
    order numbers from least to greatest
    The above sounds like a 'sort'. Do a search here for code samples that do a sort

Similar Threads

  1. Beginner's Problem on Loop/If statement
    By obdi in forum New To Java
    Replies: 2
    Last Post: 07-07-2008, 01:41 AM
  2. goto statement
    By bugger in forum New To Java
    Replies: 2
    Last Post: 12-12-2007, 04:47 PM
  3. Help with if statement
    By carl in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 07:53 AM
  4. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 04:45 PM
  5. If Statement
    By aDrizzle in forum New To Java
    Replies: 4
    Last Post: 07-08-2007, 08:55 PM

Posting Permissions

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