import java.util.*;
义乌ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18982081108(备注:SSL证书合作)期待与您的合作!
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));
}
}
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();
}
}
这样应该就好了。
冒泡排序
public static void main(String[] args) {
int [] num=new int[3];
num[0]=23;
num[1]=45;
num[2]=22;
System.out.println("数组排序前为");
for(int i=0;inum.length;i++){
System.out.print (num[i]+" ");
}
int temp;
for(int i=0;inum.length;i++){
for(int j=0;jnum.length-i-1;++j){
if(num[j]num[j+1]){
temp=num[j];
num[j]=num[j+1];
num[j+1]=temp;
}
}
}
System.out.println("\n输出数组升序后为");
for(int i=0;inum.length;i++){
System.out.print(num[i]+" ");
}
}