Results 1 to 10 of 10
  1. #1
    elmo is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default smallest number java program

    hi,
    I'm new to java.I need to write a program in which a user of the program enters a sequence of numbers and the program then prints out the smallest number that they entered. can anyone give me pseudo code/ hints as to how this can work.

    Thanks

  2. #2
    G4CKT is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    I forgot what pseudocode means but here's what I think you'd have to go about doing.

    1. Make the statement which queries the user for an input and then take the input and insert it into a variable. You might need a loop to do this with if statements if you want to take in numbers 1 at a time. You could take in a full list at once if you wanted to as well. A conversion would be needed after

    2. When you have the list of numbers, which should be in an array of some sort. Then use a for loop with a random variable like "max". The for loop is used to go through the array while another "if" statement would be used in the for loop in order to check whether the max < value. In the if statement if the value > max then you would go max == value in order to set the new value as the maximum value then the loop would go again. Then the else statement would be return max.

    3. Print the max value to the screen.

    Let me know if I'm unclear xD

  3. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    define:
    int min = scanner.nextInt

    while(scanner.hasNextInt()) do :
    int n = scanner.nextInt
    if n < min : min = n
    end while
    print min

    :)

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

    Default

    Quote Originally Posted by elmo View Post
    I'm new to java.I need to write a program in which a user of the program enters a sequence of numbers and the program then prints out the smallest number that they entered. can anyone give me pseudo code/ hints as to how this can work.
    You first; think how you would do it yourself, step by step; then try to write your method down in some sort of pseudo code.

    kind regards,

    Jos

    edit: way too slow ...
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    elmo is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    Thanks for the fast replies


    eraaa- my problem is what should i set the min to from the start. if i set it to 0 and they enter 1 then what they enter wont be the smllest when it should be
    Last edited by elmo; 11-21-2010 at 10:27 AM.

  6. #6
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    Quote Originally Posted by elmo View Post
    eraaa- my problem is what should i set the min to from the start. if i set it to 0 and they enter 1 then what they enter wont be the smllest when it should be
    Integer.MAX_VALUE ? or initialize the variable with the first user input :)

  7. #7
    elmo is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by eRaaaa View Post
    Integer.MAX_VALUE ? or initialize the variable with the first user input :)
    i dont know what Integer.MAX_VALUE means

    the problem with initialising the variable with first use input is that its inside a for loop




    sorry for the slow replies, my internet keeps dying :)

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by elmo View Post
    i dont know what Integer.MAX_VALUE means

    the problem with initialising the variable with first use input is that its inside a for loop
    You can still use the first input entered (as suggested above). You'll know when this is as it will be when your for loop index is it's lowest (usually 0, but I see that you have a for loop that starts at 1). So use an if block that checks to see what the value of the index is, and if 0 (or in your case, 1, I believe) use that input as your initial lowest value.

  9. #9
    elmo is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    You can still use the first input entered (as suggested above). You'll know when this is as it will be when your for loop index is it's lowest (usually 0, but I see that you have a for loop that starts at 1). So use an if block that checks to see what the value of the index is, and if 0 (or in your case, 1, I believe) use that input as your initial lowest value.
    ok...
    so how would I write it? how can i specify i only want it to set the entered number to "smallestNumber" first time round?


    {
    for( currentNumber = 1; amountOfNumbers >= currentNumber; currentNumber++)
    {
    System.out.println("Please number " + currentNumber + " of " + amountOfNumbers);
    number = input.nextInt();
    number = smallestNumber;

    }

  10. #10
    elmo is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    Like this?


    if( user == 2)
    {
    for( currentN = 1; amountN >= currentN; currentNr++)
    {
    System.out.println("Please enter number " + currentN + " of " + amountN);
    number = input.nextInt();
    if (currentN == 1)
    {
    smallestN = number;
    }
    else if(number < smallestN)
    {
    smallestN = number;
    }

    }
    }

    this lways says smallest no. is 0
    I cant see why this doesnt work, the logic makes sense

    thankks

    edit:found my mistake, above code now works perfect

    thanks million!!!
    Last edited by elmo; 11-21-2010 at 10:29 AM.

Similar Threads

  1. Phone number Program ..
    By Sary in forum New To Java
    Replies: 9
    Last Post: 03-17-2010, 07:15 PM
  2. A Number Converting Program!
    By WastedxYears in forum New To Java
    Replies: 2
    Last Post: 01-09-2010, 12:47 AM
  3. Return Smallest Index
    By Gamerpro in forum New To Java
    Replies: 4
    Last Post: 02-25-2009, 02:31 AM
  4. Replies: 1
    Last Post: 07-02-2008, 01:58 PM
  5. Getting smallest possible Double value
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-06-2007, 02:15 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
  •