62,621
社区成员
发帖
与我相关
我的任务
分享
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");
});
}
}

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

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;
}
}