求大佬解一题Java数组题 跪求解,Java数组题,想知道怎么写 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我写的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; }} 大佬有没有简单点的方法,就用for,if 什么什么的我们还没学到这么后面 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; } } } }}这应该很基础,都能看懂 首先,if和for都是最基础的if是判断for是循环其次,解决这道题肯定要用到if和for的,没有比较,你怎么排序 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"); }); }} jTabbedPane的选项卡不能设为圆角吗? Arrays.binarySearch的插入点问题 Mina2.0 实现P2P 请问在CMD下静止WINDOWS自带的小游戏呀 是怎样实现的 遇到这样一个奇怪的问题 今天第一次接触JAVA, 不知用什么编译环境比较好? 新手想学习,请各位高手指点一下!! Java该怎么学? 我的jbuilder没有声音 关于多态 如何压缩jdk自带的uuid的长度,但是要保证他的唯一性 求大佬帮我看看我的代码啊!!!!!!
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;
}
}
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;
}
}
}
}
}
这应该很基础,都能看懂
if是判断
for是循环
其次,解决这道题肯定要用到if和for的,没有比较,你怎么排序
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");
});
}
}