从键盘输入10个学生的数学、
英语和计算机的成绩,
然后按照平均成绩由高到低的顺序
依次输出,平均成绩相同的,
依次按照数学、英语、计算机
的成绩由低到高的顺序输出。
英语和计算机的成绩,
然后按照平均成绩由高到低的顺序
依次输出,平均成绩相同的,
依次按照数学、英语、计算机
的成绩由低到高的顺序输出。
解决方案 »
- java.lang包
- 关于异常的问题
- 谁来摆平它?(rehash()).100分任拿!!
- 关于输出的一个疑问!System.out.println()
- 如何在java程序中取得一个给定目录下的所有文件名?
- 怎么控制JSplitPane的位置?
- 插入当前时间到数据库
- 大家评论一下这两本书:《java入门经典》、《java完全探索》好吗?我正在看《核心技术卷I》,怎么觉得写的很零碎呀??请看过这些数的兄弟多多指点一下,小弟多谢了!!? //bow bow
- 如何用JAVA把汉字转化为ASCII码?
- URL对象如何知道是个目录还是个文件?
- 运用Java多线程和多媒体技术设计一个简单的益智类小游戏
- 跪求:用JAVA编译一个物理实验测量结果分析系统
import java.util.*;class Test
{
public static void main(String[] args) throws Exception
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入数学、英语、计算机成绩,以逗号分隔:");
String buffer = "";
String []temp = null;
float math = 0.0f;
float english = 0.0f;
float computer = 0.0f;
Set<Score> scoreSet = new TreeSet<Score>(); for(int i = 0; i < 10; i++){ buffer = reader.readLine();
temp = buffer.split(",");
math = Float.parseFloat(temp[0]);
english = Float.parseFloat(temp[1]);
computer = Float.parseFloat(temp[2]); Score score = new Score(math,english,computer);
score.setAvg((float)((math+english+computer)/3.0f)); scoreSet.add(score);
}
Score score = null;
Iterator<Score>it = scoreSet.iterator();
System.out.println("平均\t\t数学\t\t英语\t\t计算机\t\t");
while(it.hasNext()){
score = (Score)it.next();
System.out.println( score.avg + "\t\t" + score.math + "\t\t" + score.english + "\t\t" + score.computer );
}
}
}class Score implements Comparable
{
public float math;
public float english;
public float computer;
public float avg; public void setAvg(float avg){ this.avg = avg;
}
public Score(float math,float english,float computer){ this.math = math;
this.english = english;
this.computer = computer; } public int compareTo(Object obj){
if(obj == null){
return -1;
}
if(! (obj instanceof Score)){
return -1;
}
if(obj == this){
return 0;
} Score o = (Score)obj; //平均分不等
if( o.avg != this.avg){
return (o.avg<this.avg?1:-1);
}
//平均分相等
else{
//数学相等
if(this.math == o.math){
//英语相等
if(this.english == o.english){ if(this.computer == o.computer){
return 0;
}else{
return (this.computer > o.computer?1:-1);
}
}
//英语不等
else {
return (this.english > o.english?1:-1);
} }
//数学不等
else{
return -1;
}
}
}
}
是完整的.public class Test
{
public static void main(String[] args)
throws NumberFormatException, IOException {
for(int i=0;i<10;i++)
{
new Student();
}
}
}class Student implements Comparable<Student>
{
int x,y,z;//成绩
int t; //平均分
int i ;//学生编号
static int r = 1 ;//学生数量
static TreeSet<Student> s =new TreeSet<Student>();//所有学生的集合
BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
String str = null;
Student() throws NumberFormatException, IOException
{
i=r; //学生编号赋值
System.out.println("请输入第"+i+"个学生的数学成绩");
while(!((str=bfr.readLine()).matches("\\d{1,3}")&&Integer.parseInt(str)<120))
{
System.out.println("输入成绩不合法,成绩必须是数字并且小于120,请重新输入.");
}
x = Integer.parseInt(str);
System.out.println("请输入第"+i+"个学生的英语成绩");
while(!((str=bfr.readLine()).matches("\\d{1,3}")&&Integer.parseInt(str)<120))
{
System.out.println("输入成绩不合法,成绩必须是数字并且小于120,请重新输入.");
}
y = Integer.parseInt(str);
System.out.println("请输入第"+i+"个学生的计算机成绩");
while(!((str=bfr.readLine()).matches("\\d{1,3}")&&Integer.parseInt(str)<120))
{
System.out.println("输入成绩不合法,成绩必须是数字并且小于120,请重新输入.");
}
z = Integer.parseInt(str);
t = (x+y+z)/3;// 平均成绩
s.add(this); //学生添加到集合
r++; //学生数量增加
if(r==11) //学生数量到10个时调用打印方法
print();
}
void print()
{
Iterator i = s.iterator();
while(i.hasNext())
{
System.out.println(i.next());
}
}
public int compareTo(Student o) {
if(this.t>o.t)return -1;
if(this.t<o.t)return 1;
else
{
if(this.x>o.x)return -1;
if(this.x<o.x)return 1;
if(this.y>o.y)return -1;
if(this.y<o.y)return 1;
if(this.z>o.z)return -1;
if(this.z<o.z)return 1;
}
return 0;
}
public String toString()
{
return "第"+i+"个学生的平均成绩: "+t+"\t数学成绩: "+x+"\t英语成绩: "+y+
"\t计算机成绩: "+z;
}
}
import java.util.*;public class Demo1 { public static void main(String[] args) throws Exception {
int n = 10;
Score[] score = new Score[n];
BufferedReader buf = new BufferedReader(
new InputStreamReader(System.in));
for (int i = 0; i < n; i++) {
System.out.println("请输入第" + (i + 1) + "个 学生的成绩:数学、英语、计算机");
float math = Float.parseFloat(buf.readLine());
float english = Float.parseFloat(buf.readLine());
float computer = Float.parseFloat(buf.readLine());
Score sc = new Score(math, english, computer);
score[i] = sc;
}
Arrays.sort(score);
for (int i = 0; i < score.length; i++) {
System.out.println(score[i]);
}
}
}class Score implements Comparable<Score> {
private float english;
private float computer;
private float math;
private float avg = avgScore(english, computer, math); public Score(float math, float english, float computer) {
this.english = english;
this.computer = computer;
this.math = math;
} public float avgScore(float math, float english, float computer) {
return (english + computer + math) / 3;
} public String toString() {
return "英语成绩:" + this.english + ",计算机成绩:" + this.computer + ",数学成绩:"
+ this.math + "平均分是:" + avgScore(english, computer, math);
} public int compareTo(Score o) {
int res = this.avg > o.avg ? 1 : (this.avg == o.avg ? 0 : -1);
if (res == 0) {
res = this.math > o.math ? 1 : (this.math == o.math ? 0 : -1);
}
if (res == 0) {
res = this.english > o.english ? 1 : (this.english == o.english ? 0
: -1);
}
if (res == 0) {
res = this.computer > o.computer ? 1
: (this.computer == o.computer ? 0 : -1);
}
return res;
}
}
唉,都是让猫扑给害的~