1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

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

Discussion in 'Software Development' started by SpeedRacer5, Oct 2, 2004.

Thread Status:
Not open for further replies.
Advertisement
  1. SpeedRacer5

    SpeedRacer5 Thread Starter

    Joined:
    Feb 10, 2004
    Messages:
    201
    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
     
  2. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    Hi SpeedRacer,

    Why these lines ?
     
  3. SpeedRacer5

    SpeedRacer5 Thread Starter

    Joined:
    Feb 10, 2004
    Messages:
    201
    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.
     
  4. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    Try this :
     
  5. SpeedRacer5

    SpeedRacer5 Thread Starter

    Joined:
    Feb 10, 2004
    Messages:
    201
    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.
     
  6. SpeedRacer5

    SpeedRacer5 Thread Starter

    Joined:
    Feb 10, 2004
    Messages:
    201
    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.
     
  7. Sponsor

As Seen On
As Seen On...

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.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/280316