本文目录一览:
Java升序降序
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 数组升序排列
public static void main(String args[])
{
try
{
int i, sum=0;
FileWriter f_out=new FileWriter("data.txt");
System.out.println("请输入学生的个数");
int n=new Scanner(System.in).nextInt();
int s[]=new int [n];
for( i=0;in;i++)
{
System.out.println("请输入第"+(i+1)+"个学生的成绩");
s[i]=new Scanner(System.in).nextInt();
if (s[i]0||s[i]100)
{
System.out.println("输入错误,请重新输入");
s[i]=new Scanner(System.in).nextInt();
}
}
Arrays.sort(s);
for(int j=0;jn;j++){
f_out.write(s[j]+"\n");
}
f_out.close();
FileReader f_in=new FileReader("data.txt");
for(int c=f_in.read();c!=-1;c=f_in.read())
System.out.print((char)c+" ");
f_in.close();
}
catch(IOException e)
{
System.err.println(e);
e.printStackTrace();
}
}
这样应该就好了。
java 输入任意几个数字,怎样将他们从大到小排序?
public static void main(String[] args) { Scanner scan = new Scanner(System.in)。
}
System.out.println("从大到小输出:");
for (int m = num.length-1; m =0; m--) {
System.out.println(num[m]);//从大到小输出
}
}
String[] str = scan.nextLine().split(" "),// 输入时以空格隔开数字。
int[] num = new int[str.length];//初始化一个整型数组,长度为你输入数字的个数。
String[] str = scan.nextLine().split(" ");// 输入时以空格隔开数字
int[] num = new int[str.length];//初始化一个整型数组,长度为你输入数字的个数
for (int i = 0; i str.length; i++) {
num[i] = Integer.parseInt(String.valueOf(str[i]));//将字符转换为int型再赋给整型数组
}
Arrays.sort(num);//升序排序
System.out.println("从小到大输出:");
for (int j = 0; j num.length; j++) {
System.out.println(num[j]);//从小到大输出