I have the code given to me by my classm8 which tells the largest , 2nd largest , 3rd largest and smallest number.

but I can't understand how his code works.Can someone explain this to me?

import javax.swing.JOptionPane;
import javax.swing.JDialog;
public class exer1
{
    public static void main(String args[])
    {
        JDialog.setDefaultLookAndFeelDecorated(true);
        int a = 0, b=1000000, c=-1000000, temp = 0,d=0,temp2 = 0;
        String s1 = " ";
        for(int i=1;i<=10;i++)
        {
        int n1=Integer.parseInt(JOptionPane.showInputDialog("Enter a number"));
        s1 = s1 + " " + n1;
        
        if(i==1)
            a = n1;
        else
        {

          temp2 = d;
          if(n1>c)
           {
                d = c;
                c = n1;
                if(c>a)
                {
                   temp = a;
                   a = c;
                   c = temp;
                }
           }
          if(n1>d && n1<c)
                d = n1;
        }
        if(n1<b)
            b = n1;

        }
        JOptionPane.showMessageDialog(null,"Numbers are" + (s1) + "\nHighest number inputted is " + (a)
                + "\nSecond highest number inputted is " + (c)
                + "\nThird highest number inputted is " + (d)
                + "\nLowest number inputted is " + (b));

    }
            
}

Recommended Answers

All 5 Replies

It doesn't work, you better put it in trash

that doesn't help at all , it works on my netbeans ide 7.0.

that doesn't help at all , it works on my netbeans ide 7.0.

Net beans is an IDE. It has nothing to do with the code. If the code is correct it should work no matter how you run it.
Also if the code was given to you and you haven't written, shouldn't you ask the one that gave it to you for explanations?

It works for me too, but if your classmate (ps that's the correct English spelling) who gave it to you also wrote it then (s)he can explain it.
If not then here's a good opportunity for you to hone your Java/NetBeans skills. Use the NetBeans debugger to trace the logic flow and variable values to see how it works. You can save a bit of time by deleting temp2, because it's set but never used.

It is not about compile and run. It is about logic that is messed up there. Did you tried to enter a string or character to see what happens?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.