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

• 11-22-2010, 06:19 PM
apalacpac
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.

"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.

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);
}
}
• 11-22-2010, 06:46 PM
tashimoto
In your while loop it looks as though your integerNumber variable keeps getting written over with each new number the user enters.
Code:

integerNubmer = keyboard.nextInt();
Hope this helps! :)
Chris
• 11-22-2010, 06:56 PM
KevinWorkman
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?
• 11-22-2010, 07:47 PM
apalacpac
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?
• 11-22-2010, 07:58 PM
apalacpac
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.
• 11-22-2010, 08:33 PM
KevinWorkman
Quote:

Originally Posted by apalacpac
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.