日期:2014-05-20 浏览次数:20866 次
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");
}
}
}
}