Results 1 to 6 of 6
  1. #1
    apalacpac is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default How to obtain MAX/MIN Values through user data entered?

    Hi guys,

    New to the forum here and looking for some help.

    I'm writing a program that runs until the user enters "-99" to quit/end the program.

    So I ask them:
    "Enter an integer number or -99 to quit: "
    And they can enter any integer that they went, and how ever many times that they want. So if there entered in five different values, I would need to obtain the highest value and the lowest value in the set.

    I've got everything all set-up I think, just not coming out w/ the right output.

    Heres the HW link;
    CS 110: Program 6 Assignment for Programming Fundamentals 1

    Heres what I got...I'm also using jGrasp for my Java writing.

    import java.util.Scanner;

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


    // Declare and initialize local variables
    int integerNumber;
    int minNumber = Integer.MIN_VALUE;
    int maxNumber = Integer.MAX_VALUE;


    Scanner keyboard = new Scanner(System.in);

    // Loop until the sentinel value of -99 is entered
    System.out.print("Enter an integer number or -99 to quit: ");
    integerNumber = keyboard.nextInt();
    while (integerNumber != -99)
    {
    if (integerNumber == -99)
    {
    System.out.println("You did not enter any numbers");
    }
    else
    {
    System.out.print("Enter an integer number or -99 to quit: ");
    integerNumber = keyboard.nextInt();
    }

    }

    // Calculate and print the average test score.
    System.out.println("Largest: " + maxNumber);
    System.out.println("Smallest: " + minNumber);
    }
    }

  2. #2
    tashimoto is offline Member
    Join Date
    Sep 2010
    Location
    Oregon, usa
    Posts
    69
    Rep Power
    0

    Default

    In your while loop it looks as though your integerNumber variable keeps getting written over with each new number the user enters.
    Java Code:
    integerNubmer = keyboard.nextInt();
    Hope this helps! :)
    Chris

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

    Default

    How would you do this by hand, without a program? If you were given a piece of paper to write on and had to keep track of the largest number you were given by somebody else, what would you do?

  4. #4
    apalacpac is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    Thanks for your quick replies guys, I appreciate it.

    I also forgot to mention, that no matter what numbers I put in, or how many. I always come with this as my output:

    Largest: 2147483647
    Smallest: -2147483648

    __________________

    Kevin -
    I'm not the brightest tool in the shed, but if I were to keep the largest number given by someone else...I would probably try and keep them in reverse numerical order w/ the highest value starting first?

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

    Default

    Kevin...

    Another scenario:
    I would just write the highest and lowest number down on paper.
    And then update the numbers if there is a number that is higher or lower.

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

    Default

    Quote Originally Posted by apalacpac View Post
    Kevin...

    Another scenario:
    I would just write the highest and lowest number down on paper.
    And then update the numbers if there is a number that is higher or lower.
    Bingo. Your min and max variables are your piece of paper. You're never writing anything down on your piece of paper (actually, you're writing down only a single number), so it never gets updated.

Similar Threads

  1. draw shape with user defined values
    By cka in forum New To Java
    Replies: 4
    Last Post: 07-14-2010, 03:35 PM
  2. accessing custom data model values - how?
    By dave247 in forum New To Java
    Replies: 11
    Last Post: 03-07-2010, 12:20 PM
  3. Boggle game - verify words entered by user
    By dragonwolf in forum New To Java
    Replies: 11
    Last Post: 11-30-2009, 04:18 AM
  4. Replies: 40
    Last Post: 07-04-2008, 08:41 AM
  5. Averages of user inputed values (Need Help)
    By Zebra in forum New To Java
    Replies: 2
    Last Post: 04-16-2008, 01:51 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
  •