重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
import java.util.*;
专注于为中小企业提供成都网站制作、成都网站建设、外贸营销网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业黔西免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上千家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
public class Test {
public static void main(String[] args) {
int stuNum = 4;
int classNum = 3;
double[] score = new double[stuNum];
double sum = 0;
double[] avgrage = new double[classNum];
double[] arr = new double[classNum * stuNum];
Scanner input = new Scanner(System. in );
for (int i = 0; i classNum; i++) {
sum = 0;
System.out.println("请输入第" + (i + 1) + "个班级成绩:");
for (int j = 0; j score.length; j++) {
System.out.print("第" + (j + 1) + "个学员的成绩:");
double s = input.nextDouble();
score[j] = s;
sum += s;
arr[i * stuNum + j] = s;
}
avgrage[i] = sum / score.length;
System.out.println("第" + (i + 1) + "个班级参赛学员的平均分是:" + avgrage[i] + "\n");
}
input.close();
System.out.println("原序: " + Arrays.toString(arr));
//升序
for (int i = 0; i arr.length; i++) {
for (int j = i + 1; j arr.length; j++) {
if (arr[i] arr[j]) {
double tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
}
System.out.println("升序: " + Arrays.toString(arr));
//降序
for (int i = 0; i arr.length; i++) {
for (int j = i + 1; j arr.length; j++) {
if (arr[i] arr[j]) {
double tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
}
System.out.println("降序: " + Arrays.toString(arr));
}
}
java数组sort()方法降序方式:
用Arrays类sort()对数组元素进行降序排列
import java.util.Arrays;
class SortDemo
{
public static void main(String[] args)
{
int[] vec={0,15,-14,45,20,70};
Arrays.sort(vec);
System.out.println("/n");
System.out.println("降序排列:");
for (int i=vec.length-1;i=0 ;i-- )
{
System.out.print(vec[i]+" ");
}
}
}
直接调用jdk里面的方法Arrays.sort(args)方法。而且这个方法重载实现了多个参数,排序下标[N~M]位数字~倒叙,升序等等~
for (int i = 0; i list.size(); i++) {
for (int j = i + 1; j list.size(); j++) {
if (list.get(i) list.get(j)) {
int temp = list.get(i);
list.set(i, list.get(j));
list.set(j, temp);
}
}
}
System.out.println("升序:" + list);
for (int i = 0; i list.size(); i++) {
for (int j = i + 1; j list.size(); j++) {
if (list.get(i) list.get(j)) {
int temp = list.get(i);
list.set(i, list.get(j));
list.set(j, temp);
}
}
}
System.out.println("降序:" + list);