我不知道哪里错了。。

a1544624471 2012-09-18 10:12:06

import java.util.Scanner;

public class NewClass {
Student student = new Student();
int n;
public void init(Student student) {
System.out.println("How many students do you want to input?");
Scanner str = new Scanner(System.in);
n = str.nextInt();
student.scores = new int[n];
student.number = new String[n];
student.name = new String[n];
System.out.println("Please input " + n + " students' number,name and scores");
Scanner string = new Scanner(System.in);
Scanner string1 = new Scanner(System.in);
Scanner string2 = new Scanner(System.in);
for (int i = 0; i < n; i++) {
student.number[i] = string.nextLine();
student.name[i] = string1.nextLine();
student.scores[i] = string2.nextInt();
}
sort(student);
}

public void sort(Student student) {

String numbertemp, nametemp;
int scorestemp;
for (int i = 0; i < n - 1; i++) {
for (int j = 1; j < n; j++) {
if (
student.scores[i] < student.scores[j]) {
numbertemp = student.number[i];
student.number[i] = student.number[j];
student.number[j] = numbertemp;
nametemp = student.name[i];
student.name[i] = student.name[j];
student.name[j] = nametemp;
scorestemp = student.scores[i];
student.scores[i] = student.scores[j];
student.scores[j] = scorestemp;
}
}
}
output(student);
}

public void output(Student student) {
for (int i = 0; i < n; i++) {
System.out.println(student.name[i]);
}
}

public static void main(String args[]) {
NewClass application = new NewClass();

application.init(student);
}
}
class Student{

String number[];
String name[];
int scores[];
Student(String number[],String name[],int scores[]){
this.name = name;
this.number = number;
this.scores = scores;
}

Student() {
throw new UnsupportedOperationException("Not yet implemented");
}
}
...全文
77 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
jdjnn 2012-09-18
  • 打赏
  • 举报
回复
刚运行了一下你的代码发现不能运行 给你改了一下可以运行了 希望对你有帮助
import java.util.Scanner;

public class NewClass {
Student student = new Student();
int n;
public void init() {//这里给你改了 这里不能再定义student了
System.out.println("How many students do you want to input?");
Scanner str = new Scanner(System.in);
n = str.nextInt();
student.scores = new int[n];
student.number = new String[n];
student.name = new String[n];
System.out.println("Please input " + n + " students' number,name and scores");
Scanner string = new Scanner(System.in);
Scanner string1 = new Scanner(System.in);
Scanner string2 = new Scanner(System.in);
for (int i = 0; i < n; i++) {
student.number[i] = string.nextLine();
student.name[i] = string1.nextLine();
student.scores[i] = string2.nextInt();
}
sort(student);
}

public void sort(Student student) {

String numbertemp, nametemp;
int scorestemp;
for (int i = 0; i < n - 1; i++) {
for (int j = 1; j < n; j++) {
if (
student.scores[i] < student.scores[j]) {
numbertemp = student.number[i];
student.number[i] = student.number[j];
student.number[j] = numbertemp;
nametemp = student.name[i];
student.name[i] = student.name[j];
student.name[j] = nametemp;
scorestemp = student.scores[i];
student.scores[i] = student.scores[j];
student.scores[j] = scorestemp;
}
}
}
output(student);
}

public void output(Student student) {
for (int i = 0; i < n; i++) {
System.out.println(student.name[i]);
}
}

public static void main(String args[]) {
NewClass application = new NewClass();

application.init();//这里也改了
}
}
class Student{

String number[];
String name[];
int scores[];
Student(String number[],String name[],int scores[]){
this.name = name;
this.number = number;
this.scores = scores;
}

public Student(){

}
/*Student() {
throw new UnsupportedOperationException("Not yet implemented");
}*/
}

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧