马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
java编程:请把学生名和考试分数录入到Map中,并按分数显示前3名成绩学员的名字。package spring.exer;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
public class Exercise1 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
Map map = new TreeMap();
System.out.println("请输入名字:");
String str ;
str = s.next();
while(str != "null"){
System.out.println("请输入分数:");
int score = s.nextInt();
map.put(new Student(str,score),score);
System.out.println("请输入名字:");
str = s.next();
}
Set set = map.keySet();
for(int i = 0;i < 3;i++){
System.out.println(set);
}
}
}
class Student implements Comparable {
private String name;
private int score;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public Student(String name, int score) {
super();
this.name = name;
this.score = score;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + score;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (score != other.score)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public int compareTo(Object o) {
if(o instanceof Student){
Student s = (Student)o;
return s.score - this.score;
}
return 0;
}
}
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
public class Exercise1 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
Map map = new TreeMap();
int count = 0;
while ( count < 3 ) {
System.out.print("请输入第 " + (count + 1) + " 个学生的名字:");
String name = s.next();
System.out.print("请输入第 " + (count + 1) + " 个学生的分数:");
int score = s.nextInt();
map.put(new Student(name, score), score);
count ++;
System.out.println("\n");
}
Set set = map.keySet();
for (int i = 0; i < 3; i++) {
System.out.println(set);
}
}
}
class Student implements Comparable {
private String name;
private int score;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public Student(String name, int score) {
super();
this.name = name;
this.score = score;
}
@Override
public String toString() {
return "[ name:" + this.name + ", score:" + this.score + " ]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + score;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (score != other.score)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public int compareTo(Object o) {
if (o instanceof Student) {
Student s = (Student) o;
return s.score - this.score;
}
return 0;
}
}
|