1.从键盘输入10个学生成绩,将最高成绩存放在数组第一个,将最低成绩存放在数组最后一个,并输出所有成绩
2.将第一题改为用动态数组实现
第一题:
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int[] a=new int[10];
//依次向数组中输入值
for (int i=0; i<10;i++){
a[i]=s.nextInt();
}
for (int i = 0; i <a.length-1 ; i++) {
for (int j = 0; j <a.length-1-i ; j++) {
if(a[j]>a[j+1]){
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.print("所有学生成绩:");
for (Integer Scroe : a) {
System.out.print(Scroe+",");
}
}
第二题:
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
List<Integer> list=new ArrayList<>();
//依次向数组中输入值
for (int i=0; i<10;i++){
list.add(s.nextInt());
}
Collections.sort(list);
System.out.print("所有学生成绩:");
for (Integer Scroe : list) {
System.out.print(Scroe+",");
}
}
有帮助的话,望采纳