Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-30-2010, 06:36 PM   PM User | #1
BuhRock
New Coder

 
Join Date: Feb 2010
Posts: 24
Thanks: 4
Thanked 0 Times in 0 Posts
BuhRock is an unknown quantity at this point
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..
BuhRock is offline   Reply With Quote
Old 11-30-2010, 06:42 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 11-30-2010, 06:45 PM   PM User | #3
BuhRock
New Coder

 
Join Date: Feb 2010
Posts: 24
Thanks: 4
Thanked 0 Times in 0 Posts
BuhRock is an unknown quantity at this point
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);
			}
BuhRock is offline   Reply With Quote
Old 11-30-2010, 07:27 PM   PM User | #4
cs_student
Regular Coder

 
cs_student's Avatar
 
Join Date: Oct 2009
Location: ~/
Posts: 195
Thanks: 2
Thanked 22 Times in 22 Posts
cs_student is an unknown quantity at this point
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
__________________
Get GNU/Linux - Play Ogg - Vim
Using Arch Linux x86_64, Xorg + xmonad
cs_student is offline   Reply With Quote
Old 11-30-2010, 07:43 PM   PM User | #5
BuhRock
New Coder

 
Join Date: Feb 2010
Posts: 24
Thanks: 4
Thanked 0 Times in 0 Posts
BuhRock is an unknown quantity at this point
What do you mean correct standard format? I put it in code brackets.
BuhRock is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:27 AM.


Advertisement
Log in to turn off these ads.