6 Replies - 3350 Views - Last Post: 08 March 2009 - 07:41 PM Rate Topic: -----

#1 villalandron  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 24-September 08

Display numbers from largest to smallest

Post icon  Posted 08 March 2009 - 04:49 PM

Hello, I am making a program that asks the user to input some numbers and then I need the numbers to be displayed from largest to smallest. That is exactly the problem that I have. I don't know how to make the program display all the user inputs from largest to smallest. Here is what I have:

public class LargestandSmallest
{
	public static void main(String[ ] args)
	  {
			   int input;
			   int total = 0;

			   Scanner keyboard = new Scanner(System.in); 
					
			   System.out.print("Enter an integer number or press -99 to end: ");
			   input = keyboard.nextInt();
 
			   while (input != -99)
		  {
			   total += input;
			 
			   System.out.print("Enter an integer number or press -99 to end: ");
			   input = keyboard.nextInt();
   
		  }
			   System.out.println("The largest to smallest numbers are: " + input + ".");
			   input++;
		   	
		  }	
				
}



Thank you for your help!

Is This A Good Question/Topic? 0
  • +

Replies To: Display numbers from largest to smallest

#2 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Display numbers from largest to smallest

Posted 08 March 2009 - 04:53 PM

Just store all the numbers in a array or List, then when you want to print it out just sort it first.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Display numbers from largest to smallest

Posted 08 March 2009 - 06:41 PM

No array really needed if you just want the smallest and the largest

import java.util.Scanner;

public class LargestandSmallest
{
	public static void main(String[ ] args)
	{
		int input;
		int smallest = 0, largest = 0;
		boolean first = true;

		Scanner keyboard = new Scanner(System.in); 

		System.out.print("Enter an integer number or press -99 to end: ");
		input = keyboard.nextInt();

		for(;;)
		{
			
			System.out.print("Enter an integer number or press -99 to end: ");
			input = keyboard.nextInt();
			if(input == -99)
				break;
			if(first) {
				smallest = largest = input;
			}
			else {
				first = false;
				if(input < smallest)
					smallest = input;
				if(input > largest)
					largest = input;
			}
				
		}
		
		if(first)
			System.out.println("No number were entered");
		else
			System.out.println("The largest: " + largest + " the smallest: " + smallest);
	

	}	

}


Was This Post Helpful? 0
  • +
  • -

#4 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Display numbers from largest to smallest

Posted 08 March 2009 - 06:44 PM

Quote

No array really needed if you just want the smallest and the largest


Thats true, but you do need an array to print from largest to smallest ;)
Was This Post Helpful? 0
  • +
  • -

#5 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Display numbers from largest to smallest

Posted 08 March 2009 - 06:48 PM

An ArrayList would work best, i love the sortedIntList method, does all the work for you, its nice to be lazy :P
Was This Post Helpful? 0
  • +
  • -

#6 villalandron  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 24-September 08

Re: Display numbers from largest to smallest

Posted 08 March 2009 - 07:13 PM

I tried that method without the arraylist and it doesn't display the numbers from largest from smallest. It actually displays the last number entered. Here is what I have:

public class LargestandSmallest
{
	public static void main(String[ ] args)
	  {
			   
			   int input;
			   int smallest = 0, largest = 0;
			   boolean first = true;

			   Scanner keyboard = new Scanner(System.in); 
					
			   System.out.print("Enter an integer number or press -99 to end: ");
			   input = keyboard.nextInt();
 
			   for (;;)
		  {
			   
			   System.out.print("Enter an integer number or press -99 to end: ");
			   input = keyboard.nextInt();
			   
			   if(input == -99)
			   break;

			   if(first)
		  {
			   smallest = largest = input;
		  }

			   else
		  {
			   first = false;
			   if(input < smallest)
				 smallest = input;
			   if(input > largest)
				 largest = input;
		  }
		  }

			   if(first)
			   
			   System.out.println("The largest: " + largest + " the smallest " + smallest);
			   		   	
		  }	
				
}




The numbers I entered were 8, 4, and 12 and the final message was: The largest: 12, the smallest: 12. Somebody else mentioned arraylist and someone else mentioned another method. What is the simplest method? Since I am a noob, I started to read the chapter that talked about arrays since it is what that is still not covered in the class and I can't get it to work. Any help is greatly appreciated!
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Display numbers from largest to smallest

Posted 08 March 2009 - 07:41 PM

Oups... my mistake

first = false; at the wrong place

this should work:

		for (;;)
		{

			System.out.print("Enter an integer number or press -99 to end: ");
			input = keyboard.nextInt();

			if(input == -99)
				break;

			if(first)
			{
				smallest = largest = input;
				first = false;
			}

			else
			{
				if(input < smallest)
					smallest = input;
				if(input > largest)
					largest = input;
			}
		}


Was This Post Helpful? 1

Page 1 of 1