简体   繁体   中英

Ascending Numbers in Java

I'm trying to get the numbers in ascending order. When I go to console and type a number, the number prints but not in ascending order, just the number i put in.What am I doing wrong?

import java.util.*;
public class BigLittle{
   public static void main(String[]args){
      Scanner in=new Scanner(System.in);

      int a=0;
      int b=0;
      int c=0;

      System.out.print("Enter 3 Numbers");
      a=in.nextInt();
      b=in.nextInt();
      c=in.nextInt();

      if (a>b&&b>c)
         System.out.print(c);
      if (a>c) {
        System.out.print(b);
        System.out.print(a); 
      }

      if (b<a&&b<c)
        System.out.print(b);
      else {
        System.out.print(a);
        System.out.print(c);
      }
      if (c>a&&a<b)
         System.out.print(a);
      if (b<c){
         System.out.print(c+b+a);
      }
   }
}

Put the numbers from input into a List and then sort the List:

    List<Integer> list = new ArrayList<>();

    Scanner in=new Scanner(System.in);

    System.out.print("Enter 3 Numbers: ");
    list.add(in.nextInt());
    list.add(in.nextInt());
    list.add(in.nextInt());

    Collections.sort(list);

    for ( Integer i : list )
    {
        System.out.println(i);
    }

First of all, if c = 8 , b = 4 and a = 1 if will print 13 with that line System.out.print(c+b+a);

Also, if a=b=c nothing prints, or any combinaisons that a=b , b=c or a=c , there is some printing undefined.

If you want a simple architecture, try to put an array or 3 variables to put the values in it and then print only at the end of the computation.

With three arguments

If you enter three numbers, it means that you have 6 possible permutations 3!=3*2*1) which lead to 6 if-cases to be handle:

  1. a>b && b>c // by transitivity a>c . Which mean that a>b && b>c is same as saying a>b && b>c && a>c
  2. a>b && b<=c && c>b
  3. a>b && b<=c && c<=b
  4. a<=b && b>c && c>b
  5. a<=b && b>c && c<=b
  6. a<=b && b<=c // by transitivity a<=c Which mean that a<=b && b<=c is same as saying a<=b && b<=c && a<=c

Translated into code, it looks like (I let you complete the TODO ):

if (a > b) {
    if (b > c) {
        // 1. a>b && b>c
        // TODO complete here
    } else {
        if (c>b) {
            // 2. a>b && b<=c && c>b
            // TODO complete here
        } else {
            // 3. a>b && b<=c && c<=b
        }
    }
} else {
    if (b > c) {
        if (c > b) {
            // 4. a<=b && b>c && c>b
            // TODO complete here
        } else {
            // 5. a<=b && b>c && c<=b
        }
    } else {
        // 6. a<=b && b<=c
        // TODO complete here
    }
}

What's wrong in your code?

  • if a>b && b>c , then is implies a>c . It is what we call transitivity . So if your first if(a>b && b>c) is true , the second if(a>c) will be also true .
  • c+b+a is an addition (when the type is Int ), so print(c+b+a) makes no sense to me when sorting a , b and c .
  • what are you doing when a=b or b=c or a=c ? Your code will not print anything
  • if a<b , a<c and b<c then you will print a , b and again a , and then c+b+a .
  • ...

With n arguments/inputs

Now imagine that your user has to enter 10 or even worst 1000 numbers. This nested if-then-else would not be humanly readable. A better solution is to store into an array and then sort this array, like Collections.sort(yourArrayToSort);

Reference : Java Collection

For three distinct numbers a, b, c there are 6 possible permutations:

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

Let's start by using the relationship between a & b to partition these into 2 sets

a < b 
    1 2 3
    1 3 2
    2 3 1    
a > b
    2 1 3
    3 1 2
    3 2 1

Now let's further divide these 2 sets by considering the relationship between b & c :

a < b 
  b < c
    1 2 3
  b > c
    1 3 2
    2 3 1    
a > b
  b < c
    2 1 3
    3 1 2
  b > c
    3 2 1

We see that in cases where (a < b and b < c) and (a > b and b > c) we can uniquely determine the ordering. For the remaining cases we need to consider the relationship between a & c

a < b 
  b < c
    1 2 3
  b > c
    a < c
      1 3 2
    a > c
      2 3 1    
a > b
  b < c
    a < c
      2 1 3
    a > c
      3 1 2        
  b > c
    3 2 1

From this we can develop the following pseudocode:

if a < b
  if b < c
    order: a b c
  else 
    if a < c
      order: a c b
    else
      order: c a b
else
  if b < c
    if a < c
      order: b a c
    else
      order: b c a
  else
    order: c b a

Which you should be able to turn into Java code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM