gpt4 book ai didi

java - 排序三个命令行整数

转载 作者:行者123 更新时间:2023-11-29 07:38:18 26 4
gpt4 key购买 nike

你好,我正在尝试编写一个代码,从命令行获取三个整数并将它们排序为最小值、中间值和最大值。我无法弄清楚中间编程。它不会总是正确地对它们进行排序。你能帮我吗?

public class SortInteger{

public static int max3(int a, int b, int c) {
int max = a;
if (b > max) max = b;
if (c > max) max = c;
return max;
}

public static int min3(int a, int b, int c) {
int min = a;
if (b < min) min = b;
if (c < min) min = c;
return min;}

public static int sort(int a, int b, int c){
int sort = a;
if (sort > b && sort < c) sort = a;
else sort = b;
if (sort > a && sort < c) sort = b;
else sort =c;
if (sort > c && sort < a) sort = c;
else sort =b;
if (sort > c && sort < b) sort = c;
else sort = b;
if (sort > a && sort < b) sort = c;
else sort = c;
return sort;
}


public static void main(String[] args){
int a= Integer.parseInt(args [0]);
int b=Integer.parseInt(args[1]);
int c=Integer.parseInt(args[2]);
StdOut.println("Min is " + min3(a, b, c));
StdOut.println("Mid is " + sort(a, b, c));
StdOut.println("Max is " + max3(a, b, c));


}

最佳答案

尝试:

public static int mid(int a, int b, int c){
return a + b + c - max(a,b,c) - min(a,b,c);
}

同样对于 minmax 只需使用 Math:

public static int min(int a, int b, int c){
return Math.min(Math.min(a,b),c);//Replace with Math.max for max.
}

关于java - 排序三个命令行整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33028858/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com