Results 1 to 8 of 8
  1. #1
    Taszk is offline Member
    Join Date
    May 2011
    Posts
    29
    Rep Power
    0

    Default Need help with my java program.

    I'm trying to write a program that keeps getting numbers from the user, until the user enters a 0.
    The program then prints the smallest two numbers that were entered, and the largest number that was entered.
    Example:
    User enters the numbers 15, 33, 26, 12, 19, -5, 98, 12, 2, 0
    The output will be
    "Largest number:" 98
    "Smallest number:" -5
    "Second smallest number:" 2

    So far this is what I've got but I'm lost now.

    import java.util.*;
    public class Beta{
    public static void main(String[] args){
    int num;
    Scanner in=new Scanner(System.in);
    System.out.println("Enter a number:");
    num=in.nextInt();
    for
    if

    System.out.println("Largest number:");
    System.out.println("Smallest number:");
    System.out.println("Second smallest number:");
    }
    }

    The 'if' statement and 'for' loop i put there because i believe they would need to be used but i can't figure out what to put in them. Thanks for the help in advance. I'm new to programming so can you guys be clear please thanks.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default

    Forget about Java for the moment. Suppose I hand you over numbers, one by one; you have three boxes labeled 'largest', 'smallest' and 'second smallest'. After I've given you a number you can stick it in a box (or more than one). Then I hand you another number and another ... how would you do it by hand?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Ok, so there are several types of loops, each works in a slightly different way, and should be used in specific circumstances.

    A 'for' loop is a counting loop. You use it when you want to do something n times, and the value of n is known.

    A 'while' loop loops until a condition is met - while(x < y). Use this when the number of iterations is unknown, or if you want an infinite loop that only stops under certain conditions (hint hint).

    A 'for each' loop (also known as fast iteration) loops for as many times as there is something in a collection. This is a variation on a regular for loop, has a simpler syntax, and is ideal for doing something with every item in a list. It looks like this:
    Java Code:
    for(String s : someListOfStrings){
        //do something
    }
    A 'do while' loop is a variation on a regular while loop, except it does something first, and then checks if it should stop or continue (a normal while loop checks first, and then only does something if the conditions evaluate to true).

    Hope this helps clear up your confusion about loops and where to use them :D

  4. #4
    Taszk is offline Member
    Join Date
    May 2011
    Posts
    29
    Rep Power
    0

    Default

    Well considering that I don't know how many numbers you'd be giving me I'd collect all of them till your done then place the largest, smallest and second smallest number in the correct box.

  5. #5
    Taszk is offline Member
    Join Date
    May 2011
    Posts
    29
    Rep Power
    0

    Default

    Thanks, definitely cleared my understanding of loops.

  6. #6
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    What if, instead of going through all the numbers at the end, you looked at each one as it was handed to you and compared it to the ones already in the boxes?
    Get in the habit of using standard Java naming conventions!

  7. #7
    Taszk is offline Member
    Join Date
    May 2011
    Posts
    29
    Rep Power
    0

    Default

    I'd replace what's in the box if the following number is greater or less than what's already inside the box.

  8. #8
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    Right. So, in pseudocode:

    define three boxes
    do this:
    - get a number from the user
    - replace what's in the boxes if the number is greater or less than what's already inside
    ...until the user tells us to quit
    display what's in the boxes

    Now translate that into Java.

    This is a good way to approach every programming problem: think about it in real-world terms, write pseudocode (if only in your head), and then translate to code.
    Last edited by kjkrum; 05-19-2011 at 11:49 PM.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Call one Java Program from another Java Program
    By rajpalparyani in forum New To Java
    Replies: 3
    Last Post: 02-14-2011, 05:13 AM
  2. Replies: 4
    Last Post: 07-30-2010, 01:25 PM
  3. execute java program within java program
    By popey in forum New To Java
    Replies: 2
    Last Post: 10-22-2009, 06:32 PM
  4. Replies: 0
    Last Post: 04-04-2008, 03:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 10:33 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
  •