跪求解,Java数组题,想知道怎么写

解决方案 »

  1.   

    我写的double型,有需要的你改成int型就行package eg4;import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Scanner;public class Tests { public static void main(String[] args) {
    @SuppressWarnings("resource")
    Scanner sc = new Scanner(System.in);
    System.out.println("请输入学生总人数:");
    int n = sc.nextInt();
    ArrayList<Student> students = new ArrayList<Student>();
    for (int i = 0; i < n; i++) {
    Student student = new Student(sc.nextDouble(), sc.nextDouble());
    students.add(student);
    }
    Collections.sort(students);
    System.out.println("排序后的成绩为:");
    for (Student student : students) {
    System.out.println(student.getXuehao() + " " + student.getChengji());
    }
    }
    }
    class Student implements Comparable<Student> { private double xuehao;
    private double chengji; public Student(double xuehao, double chengji) {
    this.xuehao = xuehao;
    this.chengji = chengji; }

    public double getXuehao() {
    return xuehao;
    } public void setXuehao(double xuehao) {
    this.xuehao = xuehao;
    } public double getChengji() {
    return chengji;
    } public void setChengji(double chengji) {
    this.chengji = chengji;
    }
    @Override
    public int compareTo(Student s) {
    int key = 0;
    if (s.getChengji() < this.getChengji())
    key = 1;
    else if (s.getChengji() == this.getChengji()) {
    if (s.getXuehao() < this.getXuehao())
    key = 1;
    } return key;
    }
    }
      

  2.   

    大佬有没有简单点的方法,就用for,if 什么什么的我们还没学到这么后面
      

  3.   

    import java.util.*;public class Rank {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            int N = Integer.parseInt(input.nextLine());
            int[] sno = new int[N];
            int[] ach = new int[N];
            for (int i = 0; i < N; i++) {
                String s = input.nextLine();
                String[] num = s.split(" ");
                sno[i] = Integer.parseInt(num[0]);
                ach[i] = Integer.parseInt(num[1]);
            }
            bubbleSort(sno, ach);
            for (int i = 0; i < N; i++) {
                System.out.println(sno[i] + " " + ach[i]);
            }    }    public static void bubbleSort(int[] sno, int[] ach) {
            boolean needNextPass = true;
            for (int k = 1; k < ach.length && needNextPass; k++) {
                needNextPass = false;
                for (int i = 0; i < ach.length - k; i++) {
                    if (ach[i] > ach[i + 1]) {
                        int temp = ach[i];
                        int t = sno[i];
                        ach[i] = ach[i + 1];
                        sno[i] = sno[i + 1];
                        ach[i + 1] = temp;
                        sno[i + 1] = t;
                        needNextPass = true;
                    }            }
            }
            for (int i = 0; i < ach.length - 1; i++) {
                if (ach[i] == ach[i + 1]) {
                    if (sno[i + 1] < sno[i]) {
                        int t = sno[i];
                        sno[i] = sno[i + 1];
                        sno[i + 1] = t;
                    }
                }
            }
        }
    }
    这应该很基础,都能看懂
      

  4.   

    首先,if和for都是最基础的
    if是判断
    for是循环
    其次,解决这道题肯定要用到if和for的,没有比较,你怎么排序
      

  5.   


    import java.util.Scanner;
    import java.util.stream.IntStream;public class demo1 { public static void main(String[] args) {
    // TODO Auto-generated method stub
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        int[] s=new int[t*2];
       IntStream.range(0, t).forEach(i->{
       s[2*i]=sc.nextInt();
       s[2*i+1]=sc.nextInt();
       });
       IntStream.range(0, t).forEach(i->{
       IntStream.range(i+1, t).forEach(j->{
       if(s[2*j+1]>s[2*i+1]) {
       int k=s[2*i];
       int l=s[2*i+1];
       s[2*i]=s[2*j];
       s[2*i+1]=s[2*j+1];
       s[2*j]=k;
       s[2*j+1]=l;
       }
       });
       });
        IntStream.range(0, t).forEach(i->{
         System.out.print(s[2*i]+" "+s[2*i+1]+"\r\n");
        });
    }
    }