如何简化这段代码以便对用户输入进行降序排序。

huangapple go评论78阅读模式
英文:

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>

huangapple
  • 本文由 发表于 2020年9月16日 01:08:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63906763.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定