如何使用If语句对数字进行排序(Java) [英] How to Sort Numbers with If Statements (Java)
问题描述
我知道您可以轻松地使用数组对数字进行排序,但是我对课堂的分配是,我需要使用 if语句和非数组以降序对四个数字进行排序>.
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.
示例I/O:输入 40、52、6和7000 时,当预期输出为 7000、52、40、6 时,输出 7000、0、40、6 .
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.
谢谢.
推荐答案
这是发现第二名的一种方法:
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
结果截屏:
这篇关于如何使用If语句对数字进行排序(Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!