创建数组后如何显示该数组中的所有细节?这是我到目前为止所做的......
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Student s = new Student("", "", "", 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0);
Scanner sc = new Scanner(System.in);
System.out.println("Enter no. of Student");
int x = sc.nextInt();
Student [] studentDetails = new Student[x];
for(int i = 0; i < x; i++) {
studentDetails[i] = new Student("", "", "", 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0);
System.out.println("enter f name");
studentDetails[i].setfName(sc.nextLine());
studentDetails[i] = new Student("", "", "", 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0);
System.out.println("enter l name");
studentDetails[i].setlName(sc.nextLine());
studentDetails[i] = new Student("", "", "", 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0);
System.out.println("enter Reg no");
studentDetails[i].setStuID(sc.nextLine());
studentDetails[i] = new Student("", "", "", 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0);
System.out.println("enter Prj 1 marks");
studentDetails[i].setProjM1(sc.nextInt());
studentDetails[i] = new Student("", "", "", 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0);
System.out.println("enter ICT 1 Marks ");
studentDetails[i].setIctM1(sc.nextInt());
studentDetails[i] = new Student("", "", "", 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0);
System.out.println("enter Active P 1");
studentDetails[i].setActP1(sc.nextInt());
studentDetails[i] = new Student("", "", "", 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0);
System.out.println("enter Code Assg");
studentDetails[i].setCodeAsM(sc.nextInt());
studentDetails[i] = new Student("", "", "", 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0);
System.out.println("enter ICT 2 Marks");
studentDetails[i].setIctM2(sc.nextInt());
studentDetails[i] = new Student("", "", "", 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0);
System.out.println("enter Active P Marks");
studentDetails[i].setActP2(sc.nextInt());
studentDetails[i] = new Student("", "", "", 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0);
System.out.println("enter ICT 3 Marks");
studentDetails[i].setIctM3(sc.nextInt());
studentDetails[i] = new Student("", "", "", 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0);
System.out.println("enter Active P 3");
studentDetails[i].setActP3(sc.nextInt());
System.out.println(Student.toString(studentDetails));
}
}
}
这是学生班,
package cw;
public class Student {
private static String fName;
private static String lName;
private static String stuID;
private static int projM1;
private static int ictM1;
private static int actP1;
private static int codeAsM;
private static int ictM2;
private static int actP2;
private static int ictM3;
private static int actP3;
private static int component1Marks ;
private static int component2Marks;
private static int component3Marks;
private static double moduleMarks;
public Student(String fName, String lName, String stuID, int projM1, int ictM1, int actP1,
int codeAsM,int ictM2, int actP2, int ictM3, int actP3, int component1Marks,
int component2Marks, int component3Marks, int moduleMarks)
{
super ();
this.fName = fName;
this.lName = lName;
this.stuID = stuID;
this.projM1 = projM1;
this.ictM1 = ictM1;
this.actP1 = actP1;
this.codeAsM = codeAsM;
this.ictM2 = ictM2;
this.actP2 = actP2;
this.ictM3 = ictM3;
this.actP3 = actP3;
}
public String getfName() {
return fName;
}
public void setfName(String fName) {
this.fName = fName;
}
public String getlName() {
return lName;
}
public void setlName(String lName) {
this.lName = lName;
}
public String getStuID() {
return stuID;
}
public void setStuID(String stuID) {
this.stuID = stuID;
}
public int getProjM1() {
return projM1;
}
public void setProjM1(int projM1) {
this.projM1 = projM1;
}
public int getIctM1() {
return ictM1;
}
public void setIctM1(int ictM1) {
this.ictM1 = ictM1;
}
public int getActP1() {
return actP1;
}
public void setActP1(int actP1) {
this.actP1 = actP1;
}
public int getCodeAsM() {
return codeAsM;
}
public void setCodeAsM(int codeAsM) {
this.codeAsM = codeAsM;
}
public int getIctM2() {
return ictM2;
}
public void setIctM2(int ictM2) {
this.ictM2 = ictM2;
}
public int getActP2() {
return actP2;
}
public void setActP2(int actP2) {
this.actP2 = actP2;
}
public int getIctM3() {
return ictM3;
}
public void setIctM3(int ictM3) {
this.ictM3 = ictM3;
}
public int getActP3() {
return actP3;
}
public void setActP3(int actP3) {
this.actP3 = actP3;
}
public int getComponent1Marks() {
return component1Marks;
}
public void setComponent1Marks(int component1Marks) {
this.component1Marks = component1Marks;
}
public int getComponent2Marks() {
return component2Marks;
}
public void setComponent2Marks(int component2Marks) {
this.component2Marks = component2Marks;
}
public int getComponent3Marks() {
return component3Marks;
}
public void setComponent3Marks(int component3Marks) {
this.component3Marks = component3Marks;
}
public double getModuleMarks() {
return moduleMarks;
}
public void setModuleMarks(double moduleMarks) {
this.moduleMarks = moduleMarks;
}
public static String toString(Student[] studentDetails) {
return "Student [fName=" + fName +", Lname=" + lName + ", studentID="
+ stuID + ", marksProject1=" + projM1 + ", marksICT1="
+ ictM1 + ", ActiveP=" + actP1 + ", Code Ass="
+ codeAsM + ", ICT2=" + ictM2 + ", ActiveP2=" + actP2 + ", ICT3=" + ictM3 + ", ActiveP3="
+ actP3 + "]";
}
}
执行程序时,它会要求输入。但是当我输入值时,这就是我得到的输出,
学生[fName =,Lname =,studentID =,marksProject1 = 0,marksICT1 = 0,ActiveP = 0,Code Ass = 0,ICT2 = 0,ActiveP2 = 0,ICT3 = 0,ActiveP3 = 76]
请注意,只有正确打印的ActiveP3值!!
如何纠正这个? 我是java新手。这是我的第一个任务。
答案 0 :(得分:0)
您只应为数组中的每个条目调用一次Student构造函数:
studentDetails[i] = new Student("", "", "", 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0);
对i
的相同值调用多次会覆盖存储在数组i
位置的先前引用。
调用具有如此多参数的构造函数仅为所有参数传递默认值是没有意义的。使用无参数构造函数。
此外,toString()
类的Student
方法不需要任何参数,因为它只打印要调用的单个Student
的属性。
您应该更改通话:
System.out.println(Student.toString(studentDetails));
到
System.out.println(studentDetails[i].toString());
你的循环应如下所示:
for(int i = 0; i < x; i++) {
// this can be replaced by studentDetails[i] = new Student();
studentDetails[i] =
new Student("", "", "", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
System.out.println("enter f name");
studentDetails[i].setfName(sc.nextLine());
System.out.println("enter l name");
studentDetails[i].setlName(sc.nextLine());
System.out.println("enter Reg no");
studentDetails[i].setStuID(sc.nextLine());
System.out.println("enter Prj 1 marks");
studentDetails[i].setProjM1(sc.nextInt());
System.out.println("enter ICT 1 Marks ");
studentDetails[i].setIctM1(sc.nextInt());
System.out.println("enter Active P 1");
studentDetails[i].setActP1(sc.nextInt());
System.out.println("enter Code Assg");
studentDetails[i].setCodeAsM(sc.nextInt());
System.out.println("enter ICT 2 Marks");
studentDetails[i].setIctM2(sc.nextInt());
System.out.println("enter Active P Marks");
studentDetails[i].setActP2(sc.nextInt());
System.out.println("enter ICT 3 Marks");
studentDetails[i].setIctM3(sc.nextInt());
System.out.println("enter Active P 3");
studentDetails[i].setActP3(sc.nextInt());
System.out.println(studentDetails[i]);
}
将您的toString
方法更改为:
public String toString() {...}
答案 1 :(得分:0)
在每次迭代中只创建一个新对象,并使用Eran建议的空构造函数:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter no. of Student");
int numberOfStudents = sc.nextInt();
Student[] studentDetails = new Student[numberOfStudents];
for (int i = 0; i < numberOfStudents; i++) {
Student newStudent = new Student();
System.out.println("enter f name");
newStudent.setfName(sc.next());
System.out.println("enter l name");
newStudent.setlName(sc.next());
System.out.println("enter Reg no");
newStudent.setStuID(sc.next());
System.out.println("enter Prj 1 marks");
newStudent.setProjM1(sc.nextInt());
System.out.println("enter ICT 1 Marks ");
newStudent.setIctM1(sc.nextInt());
System.out.println("enter Active P 1");
newStudent.setActP1(sc.nextInt());
System.out.println("enter Code Assg");
newStudent.setCodeAsM(sc.nextInt());
System.out.println("enter ICT 2 Marks");
newStudent.setIctM2(sc.nextInt());
System.out.println("enter Active P Marks");
newStudent.setActP2(sc.nextInt());
System.out.println("enter ICT 3 Marks");
newStudent.setIctM3(sc.nextInt());
System.out.println("enter Active P 3");
newStudent.setActP3(sc.nextInt());
studentDetails[i] = newStudent;
System.out.println(newStudent);
}
sc.close();
}
}
将空构造函数添加到Student
课程中,不要创建变量static
并修改toString()
方法:
public class Student {
private String fName;
private String lName;
private String stuID;
private int projM1;
private int ictM1;
private int actP1;
private int codeAsM;
private int ictM2;
private int actP2;
private int ictM3;
private int actP3;
private int component1Marks;
private int component2Marks;
private int component3Marks;
private double moduleMarks;
public Student() {
}
.
.
.
@Override
public String toString() {
return "Student [fName=" + fName + ", Lname=" + lName + ", studentID=" + stuID + ", marksProject1=" + projM1
+ ", marksICT1=" + ictM1 + ", ActiveP=" + actP1 + ", Code Ass=" + codeAsM + ", ICT2=" + ictM2
+ ", ActiveP2=" + actP2 + ", ICT3=" + ictM3 + ", ActiveP3=" + actP3 + "]";
}
}
答案 2 :(得分:0)
public class Main {
public static void main(String[] args){
Student student = null;
Scanner sc = new Scanner(System.in);
System.out.println("Enter no. of Student");
int x = sc.nextInt();
Student [] studentDetails = new Student[x];
for(int i = 0; i < x; i++) {
student = new Student("", "", "", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
System.out.println("enter f name");
student.setfName(sc.nextLine());
System.out.println("enter l name");
student.setlName(sc.nextLine());
System.out.println("enter Reg no");
student.setStuID(sc.nextLine());
System.out.println("enter Prj 1 marks");
student.setProjM1(sc.nextInt());
System.out.println("enter ICT 1 Marks ");
student.setIctM1(sc.nextInt());
System.out.println("enter Active P 1");
student.setActP1(sc.nextInt());
System.out.println("enter Code Assg");
student.setCodeAsM(sc.nextInt());
System.out.println("enter ICT 2 Marks");
student.setIctM1(sc.nextInt());
System.out.println("enter Active P Marks");
student.setActP1(sc.nextInt());
System.out.println("enter ICT 3 Marks");
student.setIctM3(sc.nextInt());
System.out.println("enter Active P 3");
student.setActP1(sc.nextInt());
student = new Student(student.getfName(), student.getlName(), student.getStuID(), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
studentDetails[i] = student;
System.out.println(Student.toString(studentDetails));
}
}
}
在创建学生对象数组时,设置值学生对象并将对象添加到学生数组