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

时间:2015-09-11 20:53:25

标签: java sorting if-statement

我知道您可以轻松地使用数组对数字进行排序,但我的课程分配是我需要使用 if语句不是数组

到目前为止,这是我的代码:

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

从这段代码开始,我一直按降序排列数字,但是剩下一个数字,如零所示,所以我知道某些事情进展不顺利。

示例I / O: 40,52,6和7000 输入时, 当预期输出 7000,52,40,6 时,输出 7000,0,40,6

不确定我做错了什么,但我想知道,所以我可以得到一个不错的成绩。

谢谢。

4 个答案:

答案 0 :(得分:3)

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

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

此示例显示了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; }
}

答案 1 :(得分:1)

您的程序似乎正确地找到了第一个和最后一个位置。然而,它的逻辑在需要找到第二位的部分中断。

//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

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

所以我们希望第二位的数字是52,firstNum

  • firstNum != firstPlace?是的,它与7000不同,true
  • firstNum != fourthPlace?是的,它不同于6,true
  • firstNum < firstPlace?是的,它小于7000,true
  • firstNum > fourthPlace?是的,它大于6,true
  • firstNum > fourthNum?不,fourthNum是7000,而52不大于此。我们在这里得到false

因此,firstNum 被选为第二名,并且您的程序会中断。

secondNum的条件也以稍微不同的方式被打破。

另请注意,某些条件是多余的。如果truefirstNum < firstPlace,则firstNum != firstPlace也是如此。

所以你可以尝试修复你的条件&#39;逻辑,或者您可以尝试不同的算法。例如:

  • 仅准备四个变量,并为交换准备一个额外的临时变量。 (您可以通过执行x)来交换ytemp = x; x = y; y = temp
  • 将第二个,第三个和第四个变量分别与第一个进行比较。如果它们中的任何一个大于第一个,则用它们交换它们。这将保证第一个是最好的(你明白为什么?)
  • 以相同的方式比较第三和第四到第二。现在它保证第二个是其余数字中最大的一个。
  • 将第三个和第四个相互比较。

答案 2 :(得分:1)

这是发现第二名的一种方式:

jquery.msDropDown

结果截图:

enter image description here

答案 3 :(得分:0)

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

您必须交换多少次? 从12与11、11与10、10与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)和fournum(9)进行比较)
  • 第四次交换10,11,9,12(从最左边再次开始;从firstnum(11)和secondnum(10)之间的比较开始)
  • 第五次交换10,9,11,12(通过对secondnum(11)和第一个num(9)的比较)
  • 第六次交换9,10,11,12(通过对firstnum(10)和secondnum(9)的比较)

现在交换过程已经完成,因此您将需要交换六次,以便按升序对四个输入进行排序。我这样做是因为我收到了以这种方式进行排序的问题,但是如果您希望以降序进行排序,则只需将“>”更改为“ <”。您可以查找并尝试了解交换过程。前面的评论已经简要地描述了。

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

}