Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Feb 2010
    Posts
    24
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Help with While Loop

    I am writing a program that uses a while loop to determine the largest number input so far. I have to input 10 numbers in whole. At the end of the loop, it should output the two largest number.

    Code:
    import java.util.Scanner;
    
    public class Largest
    {
    	public static void main(String[] args)
    	{
    		int counter = 1;
    		int number;
    		int largest = 0;
    		int number2;
    		
    		Scanner input = new Scanner(System.in);
    		
    		while (counter < 10)
    		{
    			System.out.println("Enter a number");
    			number = input.nextInt();
                      
                       // ???
                    }
    Last edited by BuhRock; 11-30-2010 at 06:40 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Use conditions to check if number > largest. If it is, push the largest into the second largest (number2 maybe?). Otherwise, check if number > second largest and replace it.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    New Coder
    Join Date
    Feb 2010
    Posts
    24
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Like this? This displays the largest number of the iteration, I need the two largest of all iterations.

    Code:
    while (counter < 10)
    		{
    			System.out.println("Enter a number");
    			number = input.nextInt();
    			
    			System.out.println("Enter another number");
    			number2 = input.nextInt();
    			
    			if (number > number2)
    				largest = number;
    			else
    				largest = number2;
    				
    				counter++;
    				
    				System.out.println(largest);
    			}

  • #4
    Regular Coder cs_student's Avatar
    Join Date
    Oct 2009
    Location
    ~/
    Posts
    195
    Thanks
    2
    Thanked 22 Times in 22 Posts
    What you want to do is have two variables which store the two largest of all iterations. In each iteration you want to go through and see if the number in the current iteration is larger than one of the two. If it is, then you want to change the corresponding variable so it reflects the new largest number.

    Also, when posting code, please try to use the correct standard formatting

  • #5
    New Coder
    Join Date
    Feb 2010
    Posts
    24
    Thanks
    4
    Thanked 0 Times in 0 Posts
    What do you mean correct standard format? I put it in code brackets.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •