How to Sort Numbers with If Statements (Java)

走远了吗. 提交于 2021-02-16 16:05:54

问题


I know that you can easily sort numbers with an array, but my assignment for class is that I need to sort four numbers in descending order using if statements and not arrays.

Here is my code so far:

package integersort;

import java.util.Scanner;

public class IntegerSort {

public static void main(String[] args) {
    Scanner userInput = new Scanner(System.in);

    int firstNum, secondNum, thirdNum, fourthNum; //inputted numbers

    System.out.println("Enter first number:");
    firstNum = userInput.nextInt();
    System.out.println("Enter second number:");
    secondNum = userInput.nextInt();
    System.out.println("Enter third number:");
    thirdNum = userInput.nextInt();
    System.out.println("Enter fourth number:");
    fourthNum = userInput.nextInt();


    int firstPlace = 0, secondPlace = 0, thirdPlace = 0, fourthPlace = 0;

    //firstPlace start
    if (firstNum > secondNum && firstNum > thirdNum && firstNum > fourthNum) {
        firstPlace = firstNum;
    } else if (secondNum > firstNum && secondNum > thirdNum && secondNum > fourthNum) {
        firstPlace = secondNum;
    } else if (thirdNum > firstNum && thirdNum > secondNum && thirdNum > fourthNum) {
        firstPlace = thirdNum;
    } else if (fourthNum > firstNum && fourthNum > secondNum && fourthNum > thirdNum) {
        firstPlace = fourthNum;
    }
    //firstPlace end

    //fourthPlace start
    if (firstNum < secondNum && firstNum < thirdNum && firstNum < fourthNum) {
        fourthPlace = firstNum;
    } else if (secondNum < firstNum && secondNum < thirdNum && secondNum < fourthNum) {
        fourthPlace = secondNum;
    } else if (thirdNum < firstNum && thirdNum < secondNum && thirdNum < fourthNum) {
        fourthPlace = thirdNum;
    } else if (fourthNum < firstNum && fourthNum < secondNum && fourthNum < thirdNum) {
        fourthPlace = fourthNum;
    }
    //forthPlace end

    //secondPlace start
    if (firstNum != firstPlace && firstNum != fourthPlace && firstNum < firstPlace && firstNum > fourthPlace && firstNum > fourthNum) {
        secondPlace = firstNum;
    } else if (secondNum != firstPlace && secondNum != fourthPlace && secondNum > firstNum && secondNum > thirdNum && secondNum > fourthNum) {
        secondPlace = secondNum;
    } else if (thirdNum != firstPlace && thirdNum != fourthPlace && thirdNum > firstNum && thirdNum > secondNum && thirdNum > fourthNum) {
        secondPlace = thirdNum;
    } else if (fourthNum != firstPlace && fourthNum != fourthPlace && fourthNum > firstNum && fourthNum > secondNum && fourthNum > thirdNum) {
        secondPlace = fourthNum;
    }
    //secondPlace end

    //thirdPlace start
    if (firstNum != firstPlace && firstNum != secondPlace && firstNum != fourthPlace) {
        thirdPlace = firstNum;
    } else if (secondNum != firstPlace && secondNum != secondPlace && secondNum != fourthPlace) {
        thirdPlace = secondNum;
    } else if (thirdNum != firstPlace && thirdNum != secondPlace && thirdNum != fourthPlace) {
        thirdPlace = thirdNum;
    } else if (fourthNum != firstPlace && fourthNum != secondPlace && fourthNum != fourthPlace){
        thirdPlace = fourthNum;
    }
    //thirdPlace end

    System.out.println("The sorted numbers are: "+ firstPlace + " " + secondPlace + " " + thirdPlace + " " + fourthPlace);
    }
}

From this code, I keep getting the numbers in descending order, but there is one digit remaining, as indicated with a zero just so I know something did not go well.

Example I/O: When 40, 52, 6, and 7000 are inputted, 7000, 0, 40, 6 are outputted, when the expected output is 7000, 52, 40, 6.

Not sure what I am doing wrong, but I would like to know so I can get a decent grade on this.

Thank you.


回答1:


This is one way of discovering second place:

int storeFirstNum = 0;
int storeSecondNum = 0;
int storeThirdNum = 0;
int storeFourthNum = 0;

// secondPlace start
if (firstNum != firstPlace && firstNum != fourthPlace) {
    storeFirstNum = firstNum;
}
if (secondNum != firstPlace && secondNum != fourthPlace) {
    storeSecondNum = secondNum;
}
if (thirdNum != firstPlace && thirdNum != fourthPlace) {
    storeThirdNum = thirdNum;
}
if (fourthNum != firstPlace && fourthNum != fourthPlace) {
    storeFourthNum = fourthNum;
}
if (storeFirstNum > storeSecondNum && storeFirstNum > storeThirdNum
        && storeFirstNum > storeFourthNum) {
    secondPlace = storeFirstNum;
} else if (storeSecondNum > storeFirstNum
        && storeSecondNum > storeThirdNum
        && storeSecondNum > storeFourthNum) {
    secondPlace = storeSecondNum;
} else if (storeThirdNum > storeFirstNum
        && storeThirdNum > storeSecondNum
        && storeThirdNum > storeFourthNum) {
    secondPlace = storeThirdNum;
} else if (storeFourthNum > storeFirstNum
        && storeFourthNum > storeSecondNum
        && storeFourthNum > storeThirdNum) {
    secondPlace = storeFourthNum;
}
// secondPlace end

Screenshot of outcome:




回答2:


Since a complete answer was already posted, here is another algorithm. By using a sorting network, this can be done with 5 if / swap statements. This is a c code example for descending sort of 4 numbers:

void sortnet4(int a[4])     /* four input sorting network */
{
int t;
    if (a[0] < a[2]) { t = a[0]; a[0] = a[2]; a[2] = t; }
    if (a[1] < a[3]) { t = a[1]; a[1] = a[3]; a[3] = t; }
    if (a[0] < a[1]) { t = a[0]; a[0] = a[1]; a[1] = t; }
    if (a[2] < a[3]) { t = a[2]; a[2] = a[3]; a[3] = t; }
    if (a[1] < a[2]) { t = a[1]; a[1] = a[2]; a[2] = t; }
}

This example shows an ascending order sort of 10 numbers:

void sortnet10(int a[10])   /* ten input sorting network, 29 if/swaps */
{
    int t;
    if (a[0] > a[5]) { t = a[0]; a[0] = a[5]; a[5] = t; }
    if (a[1] > a[6]) { t = a[1]; a[1] = a[6]; a[6] = t; }
    if (a[2] > a[7]) { t = a[2]; a[2] = a[7]; a[7] = t; }
    if (a[3] > a[8]) { t = a[3]; a[3] = a[8]; a[8] = t; }
    if (a[4] > a[9]) { t = a[4]; a[4] = a[9]; a[9] = t; }
    if (a[0] > a[3]) { t = a[0]; a[0] = a[3]; a[3] = t; }
    if (a[5] > a[8]) { t = a[5]; a[5] = a[8]; a[8] = t; }
    if (a[1] > a[4]) { t = a[1]; a[1] = a[4]; a[4] = t; }
    if (a[6] > a[9]) { t = a[6]; a[6] = a[9]; a[9] = t; }
    if (a[0] > a[2]) { t = a[0]; a[0] = a[2]; a[2] = t; }
    if (a[3] > a[6]) { t = a[3]; a[3] = a[6]; a[6] = t; }
    if (a[7] > a[9]) { t = a[7]; a[7] = a[9]; a[9] = t; }
    if (a[0] > a[1]) { t = a[0]; a[0] = a[1]; a[1] = t; }
    if (a[2] > a[4]) { t = a[2]; a[2] = a[4]; a[4] = t; }
    if (a[5] > a[7]) { t = a[5]; a[5] = a[7]; a[7] = t; }
    if (a[8] > a[9]) { t = a[8]; a[8] = a[9]; a[9] = t; }
    if (a[1] > a[2]) { t = a[1]; a[1] = a[2]; a[2] = t; }
    if (a[3] > a[5]) { t = a[3]; a[3] = a[5]; a[5] = t; }
    if (a[4] > a[6]) { t = a[4]; a[4] = a[6]; a[6] = t; }
    if (a[7] > a[8]) { t = a[7]; a[7] = a[8]; a[8] = t; }
    if (a[1] > a[3]) { t = a[1]; a[1] = a[3]; a[3] = t; }
    if (a[4] > a[7]) { t = a[4]; a[4] = a[7]; a[7] = t; }
    if (a[2] > a[5]) { t = a[2]; a[2] = a[5]; a[5] = t; }
    if (a[6] > a[8]) { t = a[6]; a[6] = a[8]; a[8] = t; }
    if (a[2] > a[3]) { t = a[2]; a[2] = a[3]; a[3] = t; }
    if (a[4] > a[5]) { t = a[4]; a[4] = a[5]; a[5] = t; }
    if (a[6] > a[7]) { t = a[6]; a[6] = a[7]; a[7] = t; }
    if (a[3] > a[4]) { t = a[3]; a[3] = a[4]; a[4] = t; }
    if (a[5] > a[6]) { t = a[5]; a[5] = a[6]; a[6] = t; }
}



回答3:


Your program seems to find the first and last places properly. However, its logic breaks at the part where it needs to find the second place.

//secondPlace start
if (firstNum != firstPlace && firstNum != fourthPlace && firstNum < firstPlace && firstNum > fourthPlace && firstNum > fourthNum) {
    secondPlace = firstNum;
} else if (secondNum != firstPlace && secondNum != fourthPlace && secondNum > firstNum && secondNum > thirdNum && secondNum > fourthNum) {
    secondPlace = secondNum;
} else if (thirdNum != firstPlace && thirdNum != fourthPlace && thirdNum > firstNum && thirdNum > secondNum && thirdNum > fourthNum) {
    secondPlace = thirdNum;
} else if (fourthNum != firstPlace && fourthNum != fourthPlace && fourthNum > firstNum && fourthNum > secondNum && fourthNum > thirdNum) {
    secondPlace = fourthNum;
}
//secondPlace end

Now let's look at an example where the input is 52, 40, 6, 7000.

So we expect the number that goes to the second place to be 52, the firstNum.

  • Is firstNum != firstPlace? Yes, it is different than 7000, true.
  • Is firstNum != fourthPlace? Yes, it is different than 6, true.
  • Is firstNum < firstPlace? Yes, it is smaller than 7000, true.
  • Is firstNum > fourthPlace? Yes, it is greater than 6, true.
  • Is firstNum > fourthNum? No, fourthNum is 7000, and 52 is not greater than that. So we get false here.

Thus, firstNum is not selected as the second place, and your program breaks.

The condition for secondNum is also broken, in a slightly different way.

Note also that some of the conditions are redundant. If it is true that firstNum < firstPlace, it is also true that firstNum != firstPlace.

So you can try to fix your conditions' logic, or you can try a different algorithm. For example:

  • Prepare only four variables, and one extra temporary variable for swapping. (You swap x and y by doing temp = x; x = y; y = temp).
  • Compare the second, third, and fourth variables each to the first. If any of them is greater than the first, swap them with it. This will guarantee that the first is the greatest (do you understand why?)
  • Compare the third and fourth to the second, the same way. Now it's guaranteed that the second is the greatest of the remaining numbers.
  • Compare the third and fourth to each other.



回答4:


The idea is swapping each number. Assume you have a set of numbers from highest to lowest: 12,11,10.9; I use this number because it will provide the most possible way to swap

How many times must you swap? We will check from left to right starting from comparing 12 with 11, 11 with 10, 10 with 9. If the left number is greater than the right number, we will swap it

  • Initial numbers 12,11,10,9
  • 1st swap 11,12,10,9 (From comparison between firstnum(12) and secondnum(11))
  • 2nd swap 11,10,12,9 (From comparison between secondnum(12) and thirdnum(10))
  • 3rd swap 11,10,9,12 (From comparison between thirdnum(12) and fourthnum(9))
  • 4th swap 10,11,9,12 (Start again from leftmost; from comparison between firstnum(11) and secondnum(10))
  • 5th swap 10,9,11,12 (From comparison between secondnum(11) and first num(9))
  • 6th swap 9,10,11,12 (From comparison between firstnum(10) and secondnum(9))

now the swapping process is done, so you will need to swap six times in order to sort the four input in ascending orders. I do in ascending orders because I received the problem as to sort it this way but if you want it as descending order you will just have to change the ">" to "<". You can look up and try to understand the process of swapping. The previous comments already briefly describe how.

import java.util.Scanner;
public class Q23Decisions{
public static void main(String[] args){
    Scanner input = new Scanner(System.in);
    System.out.print("Enter your four numbers: "); //Enters four number in a single line seperated with a space.
    int firstnum = input.nextInt();
    int secondnum = input.nextInt();
    int thirdnum = input.nextInt();
    int fourthnum = input.nextInt();
    int temp;

    if (firstnum > secondnum){
        temp = firstnum;
        firstnum = secondnum;
        secondnum = temp;
    }  
    // Swaps firstnum and secondnum
    if (secondnum > thirdnum){
        temp = secondnum;
        secondnum = thirdnum;
        thirdnum = temp;
    } 
    // Swaps secondnum and thirdnum
    if (thirdnum > fourthnum){
        temp = thirdnum;
        thirdnum = fourthnum;
        fourthnum = temp;
    }
    // Swaps thirdnum and fourthnum
     if (firstnum > secondnum){
        temp = firstnum;
        firstnum = secondnum;
        secondnum = temp;
    } 
    // Swaps firstnum and secondnum
    if (secondnum > thirdnum){
        temp = secondnum;
        secondnum = thirdnum;
        thirdnum = temp;
    } 
    // Swaps secondnum and thirdnum
    if (firstnum > secondnum){
        temp = firstnum;
        firstnum = secondnum;
        secondnum = temp;
    }
    // Swaps firstnum and secondnum
   System.out.println("Sorted Number: "+firstnum+" "+secondnum+" "+thirdnum+" "+fourthnum);
}

}



来源:https://stackoverflow.com/questions/32531962/how-to-sort-numbers-with-if-statements-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!