# program that determines largest and smallest

• November 21st, 2011, 05:44 PM
Reverie
program that determines largest and smallest
Hi, I'm in a beginning java course and I had to write a program that determines the largest and smallest numbers entered by the user. My teacher wanted me to use -99 as the sentinel. I got the program to do what the teacher wanted with the inputs he told us to use however I have some issues with the smallest number if the number entered was more than 500 and the largest number less than zero so I was looking for some help with how to make my program work with all numbers. (keep in mind that I only have a fairly basic knowledge of java)

Code :

```import java.util.Scanner;   public class LargestSmallest { public static void main (String[] args) { int userNumber; //user entered number int largestSoFar = 0; //Keeps track of largest number entered int smallestSoFar = 500; //Keeps track of smallest number entered final int SENTINEL = -99;   Scanner keyboard = new Scanner(System.in);   //Get number from user System.out.print("Enter a positive whole number(enter -99 to terminate): "); userNumber = keyboard.nextInt();   while (userNumber != SENTINEL) { if (userNumber > largestSoFar) largestSoFar = userNumber;   if (userNumber < smallestSoFar) smallestSoFar = userNumber;   System.out.print("Enter a positive whole number(enter -99 to terminate): "); userNumber = keyboard.nextInt(); }   System.out.println(); System.out.println("Largest number entered: " + largestSoFar); System.out.println("Smallest number entered: " + smallestSoFar); } }```
• November 21st, 2011, 06:55 PM
Junky
Re: program that determines largest and smallest
Well obviously the initial values you assign to largest and smallest are wrong. Check out the Integer class.
• February 26th, 2012, 04:56 PM
aesguitar
Re: program that determines largest and smallest
Look in the Math.* methods...
• February 26th, 2012, 07:31 PM
JonLane
Re: program that determines largest and smallest
Sorry I am going to rework my solution so it isnt so revealing, I have a bad habit of doing the work for the student
• February 26th, 2012, 07:41 PM
JonLane
Re: program that determines largest and smallest
``` int userNumber; //user entered number int largestSoFar = 0; //Keeps track of largest number entered int smallestSoFar = 500; //Keeps track of smallest number entered final int SENTINEL = -99;   //Assume user puts int inside userNumber   while (userNumber != SENTINEL) { if (userNumber > largestSoFar) largestSoFar = userNumber;   if (userNumber < smallestSoFar) smallestSoFar = userNumber;   //Assume user puts int inside userNumber }```