英文:
How can I simplify this code for sorting user input in descending order
问题
package cmpsci111;
import java.util.Scanner;
public class DecreasingOrder {
public static void main(String[] args) {
// 获取3个用户输入的整数并按降序显示
Scanner input = new Scanner(System.in);
System.out.println("输入第一个数字");
int num1 = input.nextInt();
System.out.println("输入第二个数字");
int num2 = input.nextInt();
System.out.println("输入第三个数字");
int num3 = input.nextInt();
// 将数字排序
if (num1 > num2 && num2 > num3) {
System.out.println(num1 + " " + num2 + " " + num3);
} else if (num2 > num1 && num1 > num3) {
System.out.println(num2 + " " + num1 + " " + num3);
} else if (num1 > num3 && num3 > num2) {
System.out.println(num1 + " " + num3 + " " + num2);
} else if (num2 > num3 && num3 > num1) {
System.out.println(num2 + " " + num3 + " " + num1);
} else if (num3 > num2 && num2 > num1) {
System.out.println(num3 + " " + num2 + " " + num1);
} else if (num3 > num1 && num1 > num2) {
System.out.println(num3 + " " + num1 + " " + num2);
}
}
}
英文:
So I am doing computer science and this was one of our assignments and it worked for it but I would like to know how this could be simplified as it seems messy.
import java.util.Scanner;
public class DecreasingOrder {
public static void main(String[] args) {
//get 3 user ints and display them in decreasing order
Scanner input = new Scanner(System.in);
System.out.println("Enter the first number");
int num1 = input.nextInt();
System.out.println("Enter the second number");
int num2 = input.nextInt();
System.out.println("Enter the third number");
int num3 = input.nextInt();
//sort numbers in order
if (num1 > num2 && num2 > num3) {
System.out.println(num1 + " " + num2 + " " + num3);
} else if (num2 > num1 && num1 > num3) {
System.out.println(num2 + " " + num1 + " " + num3);
} else if (num1 > num3 && num3 > num2) {
System.out.println(num1 + " " + num3 + " " + num2);
} else if (num2 > num3 && num3 > num1) {
System.out.println(num2 + " " + num3 + " " + num1);
} else if (num3 > num2 && num2 > num1) {
System.out.println(num3 + " " + num2 + " " + num1);
} else if (num3 > num1 && num1 > num2) {
System.out.println(num3 + " " + num1 + " " + num2);
}
}
}
答案1
得分: 2
使用数组并使用 Arrays.sort()
进行排序的简化版本:
Scanner input = new Scanner(System.in);
int[] nums = new int[3];
System.out.println("输入第一个数字");
nums[0] = input.nextInt();
System.out.println("输入第二个数字");
nums[1] = input.nextInt();
System.out.println("输入第三个数字");
nums[2] = input.nextInt();
Arrays.sort(nums);
System.out.println(nums[2] + " " + nums[1] + " " + nums[0]);
英文:
Simplified version, using an array and sorting it using Arrays.sort()
:
Scanner input = new Scanner(System.in);
int[] nums = new int[3];
System.out.println("Enter the first number");
nums[0] = input.nextInt();
System.out.println("Enter the second number");
nums[1] = input.nextInt();
System.out.println("Enter the third number");
nums[2] = input.nextInt();
Arrays.sort(nums);
System.out.println(nums[2] + " " + nums[1] + " " + nums[0]);
答案2
得分: 0
这里是另一个简化版本。它使用了 Java 8 的流操作。
- 创建一个扫描器对象。
- 提示输入三个整数。
- 从控制台获取三个数字。
- 将它们转换为 Integer 对象,以便按降序排序。
- 转换为数组。
- 然后打印它们。
Scanner input = new Scanner(System.in);
System.out.println("请输入三个整数:");
System.out.println(Arrays
.toString(IntStream.generate(input::nextInt).limit(3)
.boxed().sorted(Comparator.reverseOrder())
.toArray(Integer[]::new)));
英文:
Here is another simplified version. It uses Java 8 streams.
- Create a scanner object
- Prompt for three integers.
- take in three numbers from the console.
- convert them to Integer objects so they can be sorted in descending order.
- convert to an array.
- And print them.
Scanner input = new Scanner(System.in);
System.out.println("Please enter three integers.");
System.out.println(Arrays
.toString(IntStream.generate(input::nextInt).limit(3)
.boxed().sorted(Comparator.reverseOrder())
.toArray(Integer[]::new)));
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论