Results 1 to 13 of 13
  1. #1
    Art_88 is offline Member
    Join Date
    Sep 2011
    Posts
    4
    Rep Power
    0

    Default How to display largest and smallest numbers entered..?

    Hi, I'm new to java programming and I have a particular program that I'm having trouble with. This is the description of the program.

    "Write a program with a loop that lets the user enter a series of integers. The user should enter -99 to signal the end of the series. After all the numbers have been entered, the program should display the largest and smallest numbers entered."

    So far I have this:


    import java.util.Scanner;

    public class Series
    {
    public static void main(String [] args)

    {
    Scanner keyboard = new Scanner(System.in);

    System.out.print("Enter a series of integer or -99 to end the series: ");
    int integer = keyboard.nextInt();

    while(integer != -99)
    {
    System.out.print("Enter a series of integer or -99 to end the series: ");
    integer = keyboard.nextInt();
    }


    }
    }

    Ok, now I'm stuck because I can't seem to figure out how to display the largest and smallest numbers entered. Any help would be much appreciated. THank you.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default Re: How to display largest and smallest numbers entered..?

    I don't see any code that does anything with the largest or smallest number.

    How would you do this by hand, or in your head, without a computer? If somebody was going to tell you a bunch of numbers, how would you tell them the smallest and largest numbers when they were done?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default Re: How to display largest and smallest numbers entered..?

    You'll need a couple of int variables.
    One to hold the largest and one to hold the smallest.

  4. #4
    Art_88 is offline Member
    Join Date
    Sep 2011
    Posts
    4
    Rep Power
    0

    Default Re: How to display largest and smallest numbers entered..?

    thanks for the quick reply. I will try to modify this program and I hope I can figure it out thanks

  5. #5
    Join Date
    Sep 2011
    Posts
    11
    Rep Power
    0

    Default Re: How to display largest and smallest numbers entered..?

    You'll probably want to do a for loop that steps through every number in your integer variable (Which you might want to change the name of, that can get confusing). Then just compare it to the largest and smallest number to see if it is the new largest or smallest, and if it is, store it as such. Remember that your first number is always going to be both the largest and smallest.

    Two common things to forget here would be to not actually use the -99, and to account for someone making -99 the only number.
    Last edited by Taikei_no_Yuurei; 09-08-2011 at 04:36 PM.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default Re: How to display largest and smallest numbers entered..?

    Gotta love spoonfeeders...
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default Re: How to display largest and smallest numbers entered..?

    No need to loop through anything other than the while loop already in the OP (unless I've misread the problem).

  8. #8
    Join Date
    Sep 2011
    Posts
    11
    Rep Power
    0

    Default Re: How to display largest and smallest numbers entered..?

    Gotta love snide comments.

    Yeah, you could also compare the numbers as they come in if you preferred, and then simply display the answer when the -99 is entered. That's a bit more efficient.

  9. #9
    Art_88 is offline Member
    Join Date
    Sep 2011
    Posts
    4
    Rep Power
    0

    Default Re: How to display largest and smallest numbers entered..?

    Well I figured it out myself and somehow my code works. Here it is.


    import java.util.Scanner;

    public class Challenge8
    {
    public static void main(String [] args)

    {
    int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE, integer = 0;


    Scanner keyboard = new Scanner(System.in);



    while(integer != -99)
    {

    System.out.print("Enter a series of integer or -99 to end the series: ");
    integer = keyboard.nextInt();


    if(integer < min && integer != -99)
    min = integer;

    if(integer > max)
    max = integer;
    }

    System.out.println(min);
    System.out.println(max);
    }
    }



    If u guys can find any errors here pls tell me. Thank you all!

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,537
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to display largest and smallest numbers entered..?

    Type in the numbers -101, -100 and -99 and see what your program is doing ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    Art_88 is offline Member
    Join Date
    Sep 2011
    Posts
    4
    Rep Power
    0

    Default Re: How to display largest and smallest numbers entered..?

    when i typed -101, -100 and -99 the largest number is -99 but it's not suppose to read that number. So i fixed it by modifying my code.

    if(integer > max && integer != -99)
    max = integer;

    Is that what you're looking for JosAH?

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,537
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to display largest and smallest numbers entered..?

    Quote Originally Posted by Art_88 View Post
    when i typed -101, -100 and -99 the largest number is -99 but it's not suppose to read that number. So i fixed it by modifying my code.

    if(integer > max && integer != -99)
    max = integer;

    Is that what you're looking for JosAH?
    I don't know; it's your code; does it work now?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default Re: How to display largest and smallest numbers entered..?

    Quote Originally Posted by Taikei_no_Yuurei View Post
    Gotta love snide comments.
    Yeah sorry, spoonfeeders who offer sub-optimal solutions that don't help anybody and actually mislead novice programmers are a pet peeve of mine.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 11
    Last Post: 01-14-2011, 06:36 PM
  2. Array[] get smallest/largest value
    By bobocheez in forum New To Java
    Replies: 13
    Last Post: 09-09-2010, 11:16 PM
  3. Replies: 5
    Last Post: 02-07-2009, 07:48 AM
  4. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-14-2008, 11:46 PM
  5. Finding largest and smallest integer
    By mlhazan in forum New To Java
    Replies: 2
    Last Post: 01-12-2008, 10:30 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
  •