Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

Software Development Software Development
Search Search
Search for:
Tech Support Guy > > >

Java program to input 10 numbers and return the largest and 2nd largest


(!)

SpeedRacer5's Avatar
SpeedRacer5 SpeedRacer5 is offline
Member with 201 posts.
THREAD STARTER
 
Join Date: Feb 2004
Experience: Trying to learn more about hardware/software
02-Oct-2004, 02:43 PM #1
Java program to input 10 numbers and return the largest and 2nd largest
Hi. Big problem with an assignment. For some reason, when I input 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9 into this program, it tells me my largest is 9 and second is 0. Any Java experts here that can tell me how to fix this to have 8 as the result of 2nd largest?




--------------CODE----------------

import javax.swing.JOptionPane;
public class a3c4e23
{
public static void main (String args[])
{
int counter = 0;
int number;
int largest = 0;
int second = 0;
while (counter<10)
{
counter++;
number = Integer.parseInt(JOptionPane.showInputDialog("Enter integer"));
if (number >= largest)
largest=number;
else if (number < largest && second >= number)
second=number;
else
largest = largest;
second = second;
}

System.out.println("Largest number input: " + largest);
System.out.println("Second largest input: " + second); }}

--------------CODE---------------------------




Thanks a bunch,

SpeedRacer5
Chicon's Avatar
Computer Specs
Member with 6,650 posts.
 
Join Date: Jul 2004
Location: 50 34' 07.13" N - 04 10' 23.
Experience: Second socks retriever
02-Oct-2004, 03:25 PM #2
Hi SpeedRacer,

Why these lines ?
Quote:
else
largest = largest;
second = second;
SpeedRacer5's Avatar
SpeedRacer5 SpeedRacer5 is offline
Member with 201 posts.
THREAD STARTER
 
Join Date: Feb 2004
Experience: Trying to learn more about hardware/software
02-Oct-2004, 07:49 PM #3
I don't know. No clue how to make it spit out the 2nd largest numbers.

Final program for my assignment for school and it seems easier than the others, but no clue how to make it work.
Chicon's Avatar
Computer Specs
Member with 6,650 posts.
 
Join Date: Jul 2004
Location: 50 34' 07.13" N - 04 10' 23.
Experience: Second socks retriever
03-Oct-2004, 05:07 AM #4
Try this :
Quote:
while (counter<10)
{
counter++;
number = Integer.parseInt(JOptionPane.showInputDialog("Enter integer"));
if (number >= largest) largest=number;
else if (number >= second) second=number;
}
SpeedRacer5's Avatar
SpeedRacer5 SpeedRacer5 is offline
Member with 201 posts.
THREAD STARTER
 
Join Date: Feb 2004
Experience: Trying to learn more about hardware/software
03-Oct-2004, 09:16 AM #5
When I use that code and put in numbers 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, it returns Largest as 9 and Second largest as 0.

I have no clue as to why.
SpeedRacer5's Avatar
SpeedRacer5 SpeedRacer5 is offline
Member with 201 posts.
THREAD STARTER
 
Join Date: Feb 2004
Experience: Trying to learn more about hardware/software
03-Oct-2004, 10:18 AM #6
I've also set it up like this:

import javax.swing.JOptionPane;
public class a3c4e23
{
public static void main (String args[])
{
int counter = 0;
int number;
int largest = 0;
int second = 0;
while (counter<10)
{
counter++;
number = Integer.parseInt(JOptionPane.showInputDialog("Enter integer"));
if (number >= largest)
largest=number;
else if (number >= second && number <= largest)
second=number;
}

System.out.println("Largest number input: " + largest);
System.out.println("Second largest input: " + second); }}



Following the logic, I figured that if the number is bigger than second and smaller than the largest, it would set number as second. Guess not.
As Seen On

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
WELCOME TO TECH SUPPORT GUY!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.


(clock)
THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑