有一个要求用java写现有五个学生综合测评成绩如下:78分student1,60分student2,85分student3,,72分student4,91分student5,(1)创建数组,将这个分数按照上面的顺序放入数组中,(2)写一个方法,对数组中的对象按照分数从低到高排序,(3)写一个方法,对数组中再添加一个学生,并且保证数组中对象成绩从低到高的顺序,请指教,我是新手,谢谢
调试欢乐多
--------------------------
看需求好像只需要处理分数就可以了
那么直接Arrays.sort(scorearray);
...
int[] scores={78,65,65,45,89};
scores = Arrays.sort(scores);
System.out.println(Arrays.toString(scores));int newScore = 77;
int[] scores2 = new int[scores.length+1];
System.arraycopy(scores,0,scores2,0,srores.length);
scores2[scores2.length-1] = newScore;
scores2 = Arrays.sort(scores2);
System.out.println(Arrays.toString(scores2));
...
public class ArrayInit {
public static int[] main(String[] args) { int[] scores={78,65,65,45,89};
scores = Arrays.sort(scores);
System.out.println(Arrays.toString(scores));
int newScore = 77;
int[] scores2 = new int[scores.length+1];
System.arraycopy(scores,0,scores2,0,srores.length);
scores2[scores2.length-1] = newScore;
scores2 = Arrays.sort(scores2);
System.out.println(Arrays.toString(scores2));
}这样写可以吗 ,怎么有错啊
class ArrayInit {
public static void main(String[] args) { int[] scores={78,65,65,45,89};
Arrays.sort(scores);
System.out.println(Arrays.toString(scores));
int newScore = 77;
int[] scores2 = new int[scores.length+1];
System.arraycopy(scores,0,scores2,0,scores.length);
scores2[scores2.length-1] = newScore;
Arrays.sort(scores2);
System.out.println(Arrays.toString(scores2));
}
}
这个可以运行
import java.util.*;
import java.io.*;
class Student {
private String name;
private float score;
public Student(String name ,float score)
{
this.name = name;
this.score = score;
}
public Student()
{
name = null;
score = 0;
}
public String getName()
{
return name;
}
public float getScore()
{
return score;
}
public void setName(String name)
{
this.name = name;
}
public void setScore(float score)
{
this.score = score;
}
}public class Test {
public static void main(String[] args)
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Vector v = new Vector();
String line = "Start";
String name = null;
float score = 0f;
// 输入
while(!line.equalsIgnoreCase("exit"))
{
try{
System.out.print("请输入学生名:");
line = br.readLine();
if (line.equalsIgnoreCase("exit"))
break;
name = line;
System.out.print("请该生输入成绩:");
line = br.readLine();
try {
score = Float.parseFloat(line);
}catch (NumberFormatException e)
{
score = 0;
}
Student student = new Student(name,score);
v.addElement(student);
}catch (IOException e){
e.printStackTrace();
}
}
Object[] obj = v.toArray();
// 排序
Arrays.sort(obj, new Comparator() {
public int compare(Object o1, Object o2) {
Student stu1 = (Student)o1;
Student stu2 = (Student)o2;
return (int)(stu1.getScore() - stu2.getScore());
}
public boolean equals(Object obj) { return true ;}
});
// 输出
for (int i = 0; i < obj.length; i ++)
{
Student stu = (Student)obj[i];
System.out.println("学生:" + stu.getName() + " 成绩:" + stu.getScore());
}
}
}
运行输出:
F:\>javac Test.javaF:\>java Test
请输入学生名:stu1
请该生输入成绩:26
请输入学生名:stu2
请该生输入成绩:56
请输入学生名:stu3
请该生输入成绩:45
请输入学生名:stu9
请该生输入成绩:73
请输入学生名:stu8
请该生输入成绩:45
请输入学生名:exit
学生:stu1 成绩:26.0
学生:stu3 成绩:45.0
学生:stu8 成绩:45.0
学生:stu2 成绩:56.0
学生:stu9 成绩:73.0
public int compare(Object o1, Object o2) {
Student stu1 = (Student)o1;
Student stu2 = (Student)o2;
return (int)(stu2.getScore() - stu1.getScore()); // stu2 与 stu1 对换
}
public class Test{
public static void main(String[] args){
int[] scores={78,65,65,45,89};
System.out.println(Arrays.toString(scores));
Arrays.sort(scores);
System.out.println(Arrays.toString(scores));int newScore = 77;
int[] scores2 = new int[scores.length+1];
System.arraycopy(scores,0,scores2,0,scores.length);
scores2[scores2.length-1] = newScore;
Arrays.sort(scores2);
System.out.println(Arrays.toString(scores2));}}完整版,我调试过了,没问题!~
int [] scores = new int[] {78,65,65,45,89};
int newScores = 77;
public static void main(String [] args){
ArrayInit ai = new ArrayInit();
ai.arrange();
ai.insert();
}
//将原数组进行排序并打印输出
public void arrange() {
Arrays.sort(scores);
outPrint(scores);
}
//插入新的数据后再重新排序输出
public void insert() {
int [] scores2 = new int[scores.length + 1];
System.arraycopy(scores,0,scores2,0,scores.length);
scores2[scores2.length-1] = newScores;
Arrays.sort(scores2);
outPrint(scores2);
}
//打印输入
public void outPrint(int [] j) {
for(int i = 0; i< j.length; i++)
System.out.print(j[i] + " ");
System.out.println();
}
}
这个好理解一些,不过在人机交互方面欠缺
implements sun.misc.Compare {
int score;
String name;
StuSco(String sname, int iscore) {
this.name = sname;
this.score = iscore;
} public String toString() {
return name + "\t" + score;
} public int doCompare(Object ss1, Object ss2) {
return ( (StuSco) ss1).score - ( (StuSco) ss2).score;
} public int compare(StuSco x) {
return doCompare(this, x);
} public static void main(String[] args) {
StuSco list[] = new StuSco[] {
new StuSco("Student1", 70),
new StuSco("Student2", 60),
new StuSco("Student3", 40),
new StuSco("Student4", 90),
new StuSco("Student5", 50)
};
StuSco stuN = new StuSco("studentN", 88);
int len = list.length;
int index; sun.misc.Sort.quicksort(list, stuN); // order by score print(list); // debug StuSco[] array = new StuSco[len + 1];
System.arraycopy(list, 0, array, 0, len);
for (index = 0; index < list.length && list[index].compare(stuN) <= 0;
++index);
array[index] = stuN; // insert new item
if (index < len)
System.arraycopy(list, index, array, index + 1, len - index); print(array); // debug
} public static void print(StuSco[] array) {
System.out.println("---");
for (int i = 0; i < array.length; i++)
System.out.println(array[i]);
}}