Sort three numbers Write a program that reads three numbers from keyboard and displays them in descending order. this is the question . please guide me which sorting technique i could use . thanks

asked 20 Mar '15, 15:20

Muhammad%20Hassan%20Durrani's gravatar image

Muhammad Has...
4311324
accept rate: 0%


123next »

You read three numbers from console. For example: a, b, c.

You compare each other using if and print result which depend from comparing.

For example if a >= b and a >= c and b= > c you print a b c in console.

link

answered 21 Mar '15, 06:57

RabenokDmitry's gravatar image

RabenokDmitry
1263
accept rate: 14%

this is mine code . but still it has not a pass testing .

String input = null; String in = null; String a = null; String b = null; int num = 0; int number = 0; int x = 0; int z = 0;

    String m = null;
    try 
    {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        input = bufferedReader.readLine();
        number = Integer.parseInt(input);
        in = bufferedReader.readLine();
        num = Integer.parseInt(in);
        a = bufferedReader.readLine();
        x = Integer.parseInt(a);

    if(num>=number && num>=x && number>=x) m=num+" " + number+" " +x ;
    else if(x>=number && x>=num && num>=number) m=x+" "+num+" " +number;
    else if(number>=x && number>num && x>=num) m=number+" "+ x+" " +num;    
    } 
        catch (NumberFormatException ex) 
        {
            System.out.println("Not a number !");
        }   
        catch (IOException e) 
        {
        e.printStackTrace();

}
    System.out.println(m);
link

answered 26 Mar '15, 10:50

Muhammad%20Hassan%20Durrani's gravatar image

Muhammad Has...
4311324
accept rate: 0%

hey man mine is crazy plus I tried using a second class

    package com.javarush.test.level04.lesson06.task03;

/* Sort three numbers
Write a program that reads three numbers from keyboard and displays them in descending order.
*/

import java.io.*;

public class Solution
{
    public static void main(String[] args) throws Exception
    {
    //add your code here
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

    String aNumber = reader.readLine();
    String bNumber = reader.readLine();
    String cNumber = reader.readLine();

    int aIntNumber = Integer.parseInt(aNumber);
    int bIntNumber = Integer.parseInt(bNumber);
    int cIntNumber = Integer.parseInt(cNumber);

    Sortie test = new Sortie();
    int maxOfAB = test.maxBetween(aIntNumber, bIntNumber);
    int maxOfABC = test.maxBetween(cIntNumber, maxOfAB);

    int maxOfBC = test.maxBetween(bIntNumber, cIntNumber);
    int maxOfAC = test.maxBetween(aIntNumber, cIntNumber);

    if(maxOfABC == aIntNumber)
    {
        if (maxOfBC == bIntNumber)
        {
            System.out.println(aNumber + bNumber + cNumber);
        }
        else
        {
            System.out.println(aNumber + cNumber + bNumber);
        }
    }

    if(maxOfABC == bIntNumber)
    {
        if (maxOfAC == aIntNumber)
        {
            System.out.println(bNumber + aNumber + cNumber);
        }
        else
        {
            System.out.println(bNumber + cNumber + aNumber);
        }
    }

    if(maxOfABC == cIntNumber)
    {
        if (maxOfAB == aIntNumber)
        {
            System.out.println(cNumber + aNumber + bNumber);
        }
        else
        {
            System.out.println(cNumber + bNumber + aNumber);
        }
    }

}

}

package com.javarush.test.level04.lesson06.task03;

/**
 * Created by arthur on 3/30/15.
 */
public class Sortie
{
    int maxBetween( int a, int b)
    {
        if (a > b)
        {
            return a;
        } else
        {
            return b;
        }
    }
}
link

answered 28 Mar '15, 12:40

Once%20the%20King%20of%20Andorra's gravatar image

Once the Kin...
676
accept rate: 11%

My code outputs correctly but doesn't meet the test also. Did you have any luck?

link

answered 28 Mar '15, 12:42

Once%20the%20King%20of%20Andorra's gravatar image

Once the Kin...
676
accept rate: 11%

Anddd here comes the simplest form, but still not passing test.. whyyyy??? import java.io.*;

public class Solution { public static void main(String[] args) throws Exception {

    String m = null;
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

       String num1 = reader.readLine();
       int a = Integer.parseInt(num1);
       String num2 = reader.readLine();
       int b = Integer.parseInt(num2);
       String num3 = reader.readLine();
       int c = Integer.parseInt(num3);//add your code here

           if (a>b && a>c && b>c)
             m =a+" "+b+" "+c;
             else if (b>a && b>c && a>c)
             m = b+" "+a+" "+c;
             else if (c>a && c>b && b>a)
             m = c+" "+b+" "+a;
             System.out.println(m);

//add your code here

}

}

link

answered 28 Mar '15, 20:55

Zain%20Mehar's gravatar image

Zain Mehar
13
accept rate: 0%

Zain.. What's the output?

link

answered 29 Mar '15, 05:52

Once%20the%20King%20of%20Andorra's gravatar image

Once the Kin...
676
accept rate: 11%

My output is according to the required output, i.e if I input three numbers like 6 7 5 then the result will be 7 6 5.. so! where's the problem??

link

answered 30 Mar '15, 18:52

Zain%20Mehar's gravatar image

Zain Mehar
13
accept rate: 0%

don't include the " " spaces. try that...

link

answered 30 Mar '15, 19:19

Once%20the%20King%20of%20Andorra's gravatar image

Once the Kin...
676
accept rate: 11%

Nope.. then it'll not be considered as String... :(

link

answered 01 Apr '15, 19:24

Zain%20Mehar's gravatar image

Zain Mehar
13
accept rate: 0%

edited 01 Apr '15, 19:25

use the int to compare but System.out.print the string ones

link

answered 01 Apr '15, 21:13

Once%20the%20King%20of%20Andorra's gravatar image

Once the Kin...
676
accept rate: 11%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×69
×40
×36

Asked: 20 Mar '15, 15:20

Seen: 5,540 times

Last updated: 13 Sep, 01:08