Hi. I am working on a program in which the user enters a series of integers, and the program displays the highest and lowest integers after the user inputs -99. My problem is initializing the variables; I have no clue how to make my loop work in all cases, just some. For example if I input the following: 2, 3, 4, and then end with -99, 0 is my lowest number, and 4 is my highest. I know my variables are set to 0, and I have no idea how to fix this. Here's the code, help would be much appreciated.

import java.util.Scanner;

public class largeandsmall {

public static void main(String[] args) {
	
		
		int smallNumber = 0;
		int largeNumber = 0;
		int userNumber;
		
		Scanner scan = new Scanner(System.in);
		
		System.out.println("Please enter a series of numbers. Enter " + -99 + " when you " +
		"finish entering your numbers.");
		
			do {
				
				userNumber = scan.nextInt();
			    
			if (userNumber > largeNumber && userNumber > smallNumber && userNumber != -99)
				
				largeNumber = userNumber;
				
			    else if (userNumber < smallNumber && userNumber < largeNumber && userNumber !=-99)
				
			    smallNumber = userNumber;
				
				else if (userNumber == -99)
					
					System.out.println("Here are your smallest and largest values entered in order: "
					+ smallNumber + " " + largeNumber);
					
			  }     while (userNumber != -99);

	}

}

Recommended Answers

All 10 Replies

The trick is to initialize the variables that are to receive the lowest number with the highest possible number ie any number will be smaller than its first value. For the highest value initialize the save variable with the lowest possible number so that any number will be higher.

Add comments to your code describing what you are doing.

Also play computer with your logic to see what happens.
You don't know if the first number entered will be the highest or the lowest. Where will it be saved? In the largeNumber or in the smallNumber value?

Alright, I've gotten it to work well on for the small numbers. When I enter only negatives (like -2, -3, -4) however, the largest number and smallest number are always the same. I think it's something in my logic, but I can't figure it out.

import java.util.Scanner;

public class largeandsmall {

public static void main(String[] args) {
	
		
		int smallNumber = 0;
		int largeNumber = 0;
		int userNumber;
		
		Scanner scan = new Scanner(System.in);
		
		System.out.println("Please enter a series of numbers. Enter " + -99 + " when you " +
		"finish entering your numbers.");
		
			do {
				
				userNumber = scan.nextInt();
			    
				if (userNumber == -99)
					
					System.out.println("Here are your smallest and largest values entered in order: "
					+ smallNumber + " " + largeNumber);
				
				else if (smallNumber == 0 && largeNumber == 0)
					smallNumber = userNumber;
					largeNumber = userNumber;
					
			    if (userNumber < smallNumber){
			    	smallNumber = userNumber;
			    
			    	if (userNumber > largeNumber)
			    	largeNumber = userNumber;
			  
			    }
			    
				
					
			  }     while (userNumber != -99);

	}

}

I added a nested loop, but something's wrong with the logic. Sorry for no comments, I was kinda in a hurry.

That's OK about the comments, I'll wait.

try entering the numbers in this order: -4, -2, -3

Ok, I've been messing around, and sometimes it messes up the large number, but it always displays the smallest number.

What happened when you put in the numbers that I suggested above? What were the smallest and largest values?
Does the largest value always equal the last number you enter?

Yup, the largest value equals the last number I entered.

Ok what does that tell you about your code?
Specifically where is largeNumber assigned a value?

I figured it out, I misplaced the braces, here's the final working code:

import java.util.Scanner;

public class largeandsmall {

public static void main(String[] args) {


	int smallNumber = 0;
	int largeNumber = 0;
	int userNumber;

	Scanner scan = new Scanner(System.in);

	System.out.println("Please enter a series of numbers. Enter " + -99 + " when you " +
"	finish entering your numbers.");

	do {

		userNumber = scan.nextInt();

		if (userNumber == -99)
	{
			System.out.println("Here are your smallest and largest values entered in order: "
					+ smallNumber + " " + largeNumber);}
			//Defaults to 0 and 0 if no values are entered

		else if (smallNumber == 0 && largeNumber == 0)
		
		{smallNumber = userNumber;
		largeNumber = userNumber;}

		if (userNumber < smallNumber){
			smallNumber = userNumber;}

		else if(userNumber > largeNumber){
			largeNumber = userNumber;}






		} while (userNumber != -99);

}

}

Hello there! May I ask what the meaning of these lines is?

if (userNumber < smallNumber){
			    	smallNumber = userNumber;
			    
			    	if (userNumber > largeNumber)
			    	largeNumber = userNumber;
			  
			    }

It seems you are only looking for a larger number if the user number is the smallest number? I would like to suggest using else if instead when looking for the largest number (Alternatively simply removing the brackets around your if-statement). It can't be both smaller and larger (unless you enter 0, but that case sorta handles itself already)

I hope this helps!
Emil Olofsson

Edit: Ah I see you beat me to it :)

@captainperk Your placement of {} isn't standard.
Look at emilo35's code for example.
Usually the {} are on a line by themselves (at least the }) so they are very easy to see.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.