如何使用 If 语句对数字进行排序 (Java)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32531962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:19:59  来源:igfitidea点击:

How to Sort Numbers with If Statements (Java)

javasortingif-statement

提问by Cody Mathews

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 statementsand not arrays.

我知道你可以很容易地用数组对数字进行排序,但我对类的分配是我需要使用if 语句不是数组按降序对四个数字进行排序。

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 7000are inputted, 7000, 0, 40, 6are outputted, when the expected output is 7000, 52, 40, 6.

实施例I / O:40,52,6,和7000被输入, 7000,0,40,6被输出,当预期的输出是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.

谢谢你。

采纳答案by smoggers

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:

结果截图:

enter image description here

在此处输入图片说明

回答by rcgldr

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:

由于已经发布了完整的答案,这是另一种算法。通过使用排序网络,这可以通过 5 个 if / swap 语句来完成。这是 4 个数字降序排序的 ac 代码示例:

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:

此示例显示 10 个数字的升序排序:

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; }
}

回答by RealSkeptic

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.

现在让我们看一个输入为 52、40、6、7000 的示例。

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

所以我们预计排在第二位的数字是 52,即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, fourthNumis 7000, and 52 is not greater than that. So we get falsehere.
  • firstNum != firstPlace吗?是的,它比7000有所不同,true
  • firstNum != fourthPlace吗?是的,它不同于 6, true
  • firstNum < firstPlace吗?是的,它比7000更小,true
  • firstNum > fourthPlace吗?是的,它是大于6, true
  • firstNum > fourthNum吗?不,fourthNum是 7000,而 52 不大于那个。所以我们到了false这里。

Thus, firstNumis notselected as the second place, and your program breaks.

因此,firstNum没有选择的第二位,和你的程序中断。

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

的条件secondNum也被打破,方式略有不同。

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

另请注意,某些条件是多余的。如果是true这样firstNum < firstPlace,那也确实如此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 xand yby 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.
  • 只准备四个变量和一个额外的临时变量用于交换。(你交换xytemp = x; x = y; y = temp)。
  • 将第二个、第三个和第四个变量分别与第一个变量进行比较。如果它们中的任何一个大于第一个,就用它交换它们。这将保证第一个是最大的(你明白为什么吗?)
  • 以相同的方式将第三个和第四个与第二个进行比较。现在可以保证第二个是剩余数字中最大的。
  • 比较第三和第四。

回答by Paph

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

这个想法是交换每个数字。假设您有一组从高到低的数字:12、11、10.9;我使用这个数字是因为它将提供最可能的交换方式

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

你必须交换多少次?我们将从左到右检查12与11,11与10,10与9的比较。如果左边的数字大于右边的数字,我们将交换它

  • 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))
  • 初始数字 12,11,10,9
  • 第一次交换 11,12,10,9(来自 firstnum(12) 和 secondnum(11) 之间的比较)
  • 第二次交换 11,10,12,9(来自 secondnum(12) 和thirdnum(10) 之间的比较)
  • 第三次交换 11,10,9,12(来自thirdnum(12) 和fourthnum(9) 之间的比较)
  • 第 4 次交换 10,11,9,12(再次从最左边开始;来自 firstnum(11) 和 secondnum(10) 之间的比较)
  • 第 5 次交换 10,9,11,12(来自 secondnum(11) 和 first num(9) 之间的比较)
  • 第 6 次交换 9,10,11,12(来自 firstnum(10) 和 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);
}

}

}