1.冒泡排序法; 2.输出最大值:排序完后最大值最小值都出来了; import java.util.Scanner;public class TestCompar { public static void main(String[] args) { Scanner in = new Scanner(System.in); int array[] = new int[50]; System.out.println("请输入要比较的个数!"); int a = in.nextInt(); for (int i = 0; i < a; i++) { System.out.println("请输入第" + (i + 1) + "个数"); array[i] = in.nextInt(); }
for (int i = 0; i < a;i++) { for (int j = 0; j < a-1-i; j++) { if (array[j] > array[j+1]) { int temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } } }
int max = array[a-1]; System.out.println("以上输入数中最大数为:" + max); System.out.println("按从小到大的顺序是:"); for (int i = 0; i < a; i++) { System.out.print(array[i] + "\t"); } } }
//输入a个数,比较其大小。按从小到大顺序输出。并打印出最大值import java.util.*;public class TestCompar { public static void main(String[] args) { Scanner in = new Scanner(System.in); List<Integer> inputDataList = new ArrayList<>(); System.out.println("请输入要比较的个数!"); int a = in.nextInt(); for (int i = 0; i < a; i++) { System.out.println("请输入第" + (i + 1) + "个数"); inputDataList.add(in.nextInt()); } if (!inputDataList.isEmpty()) { inputDataList.sort(null); System.out.println("以上输入数中最大数为:" + inputDataList.get(inputDataList.size()-1)); System.out.println("按从小到大的顺序是:"); for (Integer inputData : inputDataList) { System.out.print(inputData + "\t"); } } } } 【比较个数为0时的输入输出】 请输入要比较的个数! 0【正常的测试用例的输入输出】 请输入要比较的个数! 10 请输入第1个数 8 请输入第2个数 6 请输入第3个数 9 请输入第4个数 7 请输入第5个数 0 请输入第6个数 3 请输入第7个数 1 请输入第8个数 2 请输入第9个数 4 请输入第10个数 5 以上输入数中最大数为:9 按从小到大的顺序是: 0 1 2 3 4 5 6 7 8 9 【比较数据存在重复数据时的输入输出】 请输入要比较的个数! 2 请输入第1个数 1 请输入第2个数 1 以上输入数中最大数为:1 按从小到大的顺序是: 1 1
2.输出最大值:排序完后最大值最小值都出来了;
import java.util.Scanner;public class TestCompar
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int array[] = new int[50];
System.out.println("请输入要比较的个数!");
int a = in.nextInt();
for (int i = 0; i < a; i++)
{
System.out.println("请输入第" + (i + 1) + "个数");
array[i] = in.nextInt();
}
for (int i = 0; i < a;i++)
{
for (int j = 0; j < a-1-i; j++)
{
if (array[j] > array[j+1])
{
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
int max = array[a-1];
System.out.println("以上输入数中最大数为:" + max);
System.out.println("按从小到大的顺序是:");
for (int i = 0; i < a; i++)
{
System.out.print(array[i] + "\t");
}
}
}
Scanner in = new Scanner(System.in);
List<Integer> inputDataList = new ArrayList<>();
System.out.println("请输入要比较的个数!");
int a = in.nextInt();
for (int i = 0; i < a; i++) {
System.out.println("请输入第" + (i + 1) + "个数");
inputDataList.add(in.nextInt());
}
if (!inputDataList.isEmpty()) {
inputDataList.sort(null);
System.out.println("以上输入数中最大数为:"
+ inputDataList.get(inputDataList.size()-1));
System.out.println("按从小到大的顺序是:");
for (Integer inputData : inputDataList) {
System.out.print(inputData + "\t");
}
}
}
}
【比较个数为0时的输入输出】
请输入要比较的个数!
0【正常的测试用例的输入输出】
请输入要比较的个数!
10
请输入第1个数
8
请输入第2个数
6
请输入第3个数
9
请输入第4个数
7
请输入第5个数
0
请输入第6个数
3
请输入第7个数
1
请输入第8个数
2
请输入第9个数
4
请输入第10个数
5
以上输入数中最大数为:9
按从小到大的顺序是:
0 1 2 3 4 5 6 7 8 9 【比较数据存在重复数据时的输入输出】
请输入要比较的个数!
2
请输入第1个数
1
请输入第2个数
1
以上输入数中最大数为:1
按从小到大的顺序是:
1 1