Results 1 to 3 of 3
  1. #1
    mlhazan is offline Member
    Join Date
    Jan 2008
    Posts
    2
    Rep Power
    0

    Default Finding largest and smallest integer

    This code shows malicious behavior.Actually I dont know what is wrong.I can solve using max and min method but I have to do using if else so Pls help me.Here is my code:


    import javax.swing.*;
    public class Max_min {
    public static void main(String[]args){
    int a=0,b=0,c=0;
    String numA,numB,numC;

    numA=JOptionPane.showInputDialog( "Enter number A");
    a=Integer.parseInt(numA);

    numB=JOptionPane.showInputDialog( "Enter number B");
    b=Integer.parseInt(numB);

    numC=JOptionPane.showInputDialog( "Enter number C");
    c=Integer.parseInt(numC);

    if(a>b)
    {
    if(b>c){
    JOptionPane.showMessageDialog(null," a is greatest and c is smallest");
    }
    else JOptionPane.showMessageDialog(null," a is greatest and b is smallest");

    }
    else if(b>a)
    {
    if(a>c){
    JOptionPane.showMessageDialog(null," b is greatest and c is smallest");
    }
    else JOptionPane.showMessageDialog(null," b is greatest and a is smallest");
    }
    else if(c>b)
    {
    if(b>a){
    JOptionPane.showMessageDialog(null," c is greatest and a is smallest");
    }
    else JOptionPane.showMessageDialog(null," c is greatest and b is smallest");

    }
    }
    }
    Last edited by mlhazan; 01-09-2008 at 04:35 AM.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import javax.swing.*;
    
    public class MaxTest {
        public static void main(String[] args) {
            int[][] vals = {
                { 7, 9, 3 }, { 9, 6, 3 }, { 6, 2, 9 },
                { 2, 3, 8 }, { 1, 3, 2 }, { 6, 1, 4 }
            };
            System.out.println(" a  b  c  greatest  smallest");
            System.out.println("----------------------------");
            for(int j = 0; j < vals.length; j++) {
                int a = vals[j][0];
                int b = vals[j][1];
                int c = vals[j][2];
                System.out.printf(" %d  %d  %d  ", a, b, c);
    
                if(a>b) {
                    if(a > c) {
                        if(b > c) {
                            System.out.println("   a         c");
                        } else {
                            System.out.println("   a         b");
                        }
                    } else {  // (c > a)
                        System.out.println("   c         b");
                    }
                } else {  // (b > a)
                    if(b > c) {
                        if(c > a) {
                            System.out.println("   b         a");
                        } else {
                            System.out.println("   b         c");
                        }
                    } else {  // (c > b)
                        System.out.println("   c         a");
                    }
                }
            }
        }
    }

  3. #3
    mlhazan is offline Member
    Join Date
    Jan 2008
    Posts
    2
    Rep Power
    0

    Default thank you

    Thank you very much for your reply.It works.

    Quote Originally Posted by hardwired View Post
    Java Code:
    import javax.swing.*;
    
    public class MaxTest {
        public static void main(String[] args) {
            int[][] vals = {
                { 7, 9, 3 }, { 9, 6, 3 }, { 6, 2, 9 },
                { 2, 3, 8 }, { 1, 3, 2 }, { 6, 1, 4 }
            };
            System.out.println(" a  b  c  greatest  smallest");
            System.out.println("----------------------------");
            for(int j = 0; j < vals.length; j++) {
                int a = vals[j][0];
                int b = vals[j][1];
                int c = vals[j][2];
                System.out.printf(" %d  %d  %d  ", a, b, c);
    
                if(a>b) {
                    if(a > c) {
                        if(b > c) {
                            System.out.println("   a         c");
                        } else {
                            System.out.println("   a         b");
                        }
                    } else {  // (c > a)
                        System.out.println("   c         b");
                    }
                } else {  // (b > a)
                    if(b > c) {
                        if(c > a) {
                            System.out.println("   b         a");
                        } else {
                            System.out.println("   b         c");
                        }
                    } else {  // (c > b)
                        System.out.println("   c         a");
                    }
                }
            }
        }
    }

Similar Threads

  1. Finding Largest Prime Factor
    By perito in forum New To Java
    Replies: 7
    Last Post: 11-08-2010, 08:25 PM
  2. Largest string value (alphabetically)
    By mew in forum New To Java
    Replies: 3
    Last Post: 12-14-2007, 05:45 PM
  3. ArrayList problem (finding largest no)
    By bugger in forum New To Java
    Replies: 3
    Last Post: 12-12-2007, 12:47 PM
  4. Getting smallest possible Double value
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-06-2007, 02:15 PM
  5. Finding largest no
    By bugger in forum New To Java
    Replies: 11
    Last Post: 11-29-2007, 12:49 PM

Posting Permissions

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