本文介绍了如何显示存储的信息在Java中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建一个数组如何显示阵列中的所有细节后?这里是我做了什么至今。

\r
\r

进口java.util.Scanner中;\r
\r
\r
公共类主要{\r
\r
\r
\r
公共静态无效的主要(字串[] args){\r
\r
学生S =新学生(,,,0,0,0,\r
0,0,0,0,0,0,0,0,0);\r
\r
扫描仪SC =新的扫描仪(System.in);\r
\r
的System.out.println(输入没有学生的。);\r
INT X = sc.nextInt();\r
\r
学生[] = studentDetails新的学生[X];\r
\r
的for(int i = 0; I< X,我++){\r
\r
studentDetails [I] =新学生(,,,0,0,0,\r
0,0,0,0,0,0,0,0,0);\r
的System.out.println(ENTER F名称);\r
studentDetails [I] .setfName(sc.nextLine());\r
\r
studentDetails [I] =新学生(,,,0,0,0,\r
0,0,0,0,0,0,0,0,0);\r
的System.out.println(输入L的名字);\r
studentDetails [I] .setlName(sc.nextLine());\r
\r
studentDetails [I] =新学生(,,,0,0,0,\r
0,0,0,0,0,0,0,0,0);\r
的System.out.println(请输入注册号);\r
studentDetails [I] .setStuID(sc.nextLine());\r
\r
studentDetails [I] =新学生(,,,0,0,0,\r
0,0,0,0,0,0,0,0,0);\r
的System.out.println(进入PRJ1马克);\r
studentDetails [I] .setProjM1(sc.nextInt());\r
\r
studentDetails [I] =新学生(,,,0,0,0,\r
0,0,0,0,0,0,0,0,0);\r
的System.out.println(进入ICT 1标记);\r
studentDetails [I] .setIctM1(sc.nextInt());\r
\r
studentDetails [I] =新学生(,,,0,0,0,\r
0,0,0,0,0,0,0,0,0);\r
的System.out.println(进入活动第1页);\r
studentDetails [I] .setActP1(sc.nextInt());\r
\r
studentDetails [I] =新学生(,,,0,0,0,\r
0,0,0,0,0,0,0,0,0);\r
的System.out.println(输入code ASSG);\r
studentDetails [I] .SET codeASM(sc.nextInt());\r
\r
studentDetails [I] =新学生(,,,0,0,0,\r
0,0,0,0,0,0,0,0,0);\r
的System.out.println(进入ICT 2分);\r
studentDetails [I] .setIctM2(sc.nextInt());\r
\r
studentDetails [I] =新学生(,,,0,0,0,\r
0,0,0,0,0,0,0,0,0);\r
的System.out.println(输入激活P标志);\r
studentDetails [I] .setActP2(sc.nextInt());\r
\r
studentDetails [I] =新学生(,,,0,0,0,\r
0,0,0,0,0,0,0,0,0);\r
的System.out.println(进入ICT 3分);\r
studentDetails [I] .setIctM3(sc.nextInt());\r
\r
studentDetails [I] =新学生(,,,0,0,0,\r
0,0,0,0,0,0,0,0,0);\r
的System.out.println(输入激活P 3);\r
studentDetails [I] .setActP3(sc.nextInt());\r
\r
的System.out.println(Student.toString(studentDetails));\r
\r
\r
}\r
\r
\r
}\r
}

\r

\r
\r

这是学生类,

\r
\r

包CW;\r
\r
公共类学生{\r
\r
私人静态字符串FNAME;\r
私人静态字符串LNAME;\r
私人静态字符串stuID;\r
\r
私有静态诠释projM1;\r
私有静态诠释ictM1;\r
私有静态诠释actP1;\r
\r
\r
私有静态诠释codeASM;\r
私有静态诠释ictM2;\r
私有静态诠释actP2;\r
\r
\r
私有静态诠释ictM3;\r
私有静态诠释actP3;\r
\r
私有静态诠释component1Marks;\r
私有静态诠释component2Marks;\r
私有静态诠释component3Marks;\r
私有静态双moduleMarks;\r
\r
\r
\r
公益助学(字符串FNAME,LNAME字符串,字符串stuID,诠释projM1,诠释ictM1,INT actP1,\r
INT codeASM,诠释ictM2,诠释actP2,诠释ictM3,诠释actP3,INT component1Marks,\r
INT component2Marks,诠释component3Marks,诠释moduleMarks)\r
{\r
超();\r
this.fName = FNAME;\r
this.lName = LNAME;\r
this.stuID = stuID;\r
\r
this.projM1 = projM1;\r
this.ictM1 = ictM1;\r
this.actP1 =​​ actP1;\r
\r
\r
\r
这codeASM = codeASM;\r
this.ictM2 = ictM2;\r
this.actP2 = actP2;\r
\r
this.ictM3 = ictM3;\r
this.actP3 = actP3;\r
\r
}\r
\r
\r
\r
公共字符串getfName(){\r
返回FNAME;\r
}\r
\r
\r
\r
公共无效setfName(字符串FName参数){\r
this.fName = FNAME;\r
}\r
\r
\r
\r
公共字符串getlName(){\r
返回LNAME;\r
}\r
\r
\r
\r
公共无效setlName(字符串LNAME){\r
this.lName = LNAME;\r
}\r
\r
\r
\r
公共字符串getStuID(){\r
返回stuID;\r
}\r
\r
\r
\r
公共无效setStuID(字符串stuID){\r
this.stuID = stuID;\r
}\r
\r
\r
\r
公众诠释getProjM1(){\r
返回projM1;\r
}\r
\r
\r
\r
公共无效setProjM1(INT projM1){\r
this.projM1 = projM1;\r
}\r
\r
\r
\r
公众诠释getIctM1(){\r
返回ictM1;\r
}\r
\r
\r
\r
公共无效setIctM1(INT ictM1){\r
this.ictM1 = ictM1;\r
}\r
\r
\r
\r
公众诠释getActP1(){\r
返回actP1;\r
}\r
\r
\r
\r
公共无效setActP1(INT actP1){\r
this.actP1 =​​ actP1;\r
}\r
\r
\r
\r
公众诠释得到codeASM(){\r
返回codeASM;\r
}\r
\r
\r
\r
公共无效设置codeASM(INT codeASM){\r
这codeASM = codeASM;\r
}\r
\r
\r
\r
公众诠释getIctM2(){\r
返回ictM2;\r
}\r
\r
\r
\r
公共无效setIctM2(INT ictM2){\r
this.ictM2 = ictM2;\r
}\r
\r
\r
\r
公众诠释getActP2(){\r
返回actP2;\r
}\r
\r
\r
\r
公共无效setActP2(INT actP2){\r
this.actP2 = actP2;\r
}\r
\r
\r
\r
公众诠释getIctM3(){\r
返回ictM3;\r
}\r
\r
\r
\r
公共无效setIctM3(INT ictM3){\r
this.ictM3 = ictM3;\r
}\r
\r
\r
\r
公众诠释getActP3(){\r
返回actP3;\r
}\r
\r
\r
\r
公共无效setActP3(INT actP3){\r
this.actP3 = actP3;\r
}\r
\r
\r
\r
公众诠释getComponent1Marks(){\r
返回component1Marks;\r
}\r
\r
\r
\r
公共无效setComponent1Marks(INT component1Marks){\r
this.component1Marks = component1Marks;\r
}\r
\r
\r
\r
公众诠释getComponent2Marks(){\r
返回component2Marks;\r
}\r
\r
\r
\r
公共无效setComponent2Marks(INT component2Marks){\r
this.component2Marks = component2Marks;\r
}\r
\r
\r
\r
公众诠释getComponent3Marks(){\r
返回component3Marks;\r
}\r
\r
\r
\r
公共无效setComponent3Marks(INT component3Marks){\r
this.component3Marks = component3Marks;\r
}\r
\r
\r
\r
公共双getModuleMarks(){\r
返回moduleMarks;\r
}\r
\r
\r
\r
公共无效setModuleMarks(双moduleMarks){\r
this.moduleMarks = moduleMarks;\r
}\r
\r
公共静态字符串的ToString(学生[] studentDetails){\r
回到学生[FNAME =+ FNAME +,LNAME =+ LNAME +,studentID =\r
+ stu​​ID +,marksProject1 =+ projM1 +,marksICT1 =\r
+ ictM1 +,ActiveP =+ actP1 +,code驴=\r
+ codeASM +,ICT2 =+ ictM2 +,ActiveP2 =+ actP2 +,ICT3 =+ ictM3 +,ActiveP3 =\r
+ actP3 +];\r
}\r
}

\r

\r
\r

执行程序时,它要求输入。但是当我输入值,这是输出我得到,,

学生[FNAME =,LNAME =,studentID =,marksProject1 = 0,marksICT1 = 0,ActiveP = 0,code驴= 0,ICT2 = 0,ActiveP2 = 0,ICT3 = 0,ActiveP3 = 76]

请注意,只有ActiveP3值正确打印!

如何纠正呢?
我是新来java.this是我的第一项任务。


解决方案

在每次迭代创建只有一个新对象,并用一个空的构造为叶兰建议:

 进口java.util.Scanner中;公共类主要{    公共静态无效的主要(字串[] args){
        扫描仪SC =新的扫描仪(System.in);        的System.out.println(输入没有学生的。);        INT numberOfStudents = sc.nextInt();
        学生[] = studentDetails新的学生[numberOfStudents]        的for(int i = 0; I< numberOfStudents;我++){
            学生newStudent =新学生();            的System.out.println(ENTER F名称);
            newStudent.setfName(sc.next());
            的System.out.println(输入L的名字);
            newStudent.setlName(sc.next());
            的System.out.println(请输入注册号);
            newStudent.setStuID(sc.next());
            的System.out.println(进入PRJ1马克);
            newStudent.setProjM1(sc.nextInt());
            的System.out.println(进入ICT 1标记);
            newStudent.setIctM1(sc.nextInt());
            的System.out.println(进入活动第1页);
            newStudent.setActP1(sc.nextInt());
            的System.out.println(输入code ASSG);
            newStudent.set codeASM(sc.nextInt());
            的System.out.println(进入ICT 2分);
            newStudent.setIctM2(sc.nextInt());
            的System.out.println(输入激活P标志);
            newStudent.setActP2(sc.nextInt());
            的System.out.println(进入ICT 3分);
            newStudent.setIctM3(sc.nextInt());
            的System.out.println(输入激活P 3);
            newStudent.setActP3(sc.nextInt());            studentDetails [I] = newStudent;            的System.out.println(newStudent);
        }        sc.close();
    }}

空构造添加到您的学生类,不要使变量静态并修改的toString()方法:

 公共类学生{    私人字符串FNAME;
    私人字符串LNAME;
    私人字符串stuID;
    私人诠释projM1;
    私人诠释ictM1;
    私人诠释actP1;
    私人诠释codeASM;
    私人诠释ictM2;
    私人诠释actP2;
    私人诠释ictM3;
    私人诠释actP3;
    私人诠释component1Marks;
    私人诠释component2Marks;
    私人诠释component3Marks;
    私人双moduleMarks;    公益助学(){
    }    。
    。
    。    @覆盖
    公共字符串的toString(){
        回到学生[FNAME =+ FNAME +,LNAME =+ LNAME +,studentID =+ stu​​ID +,marksProject1 =+ projM1
                +,marksICT1 =+ ictM1 +,ActiveP =+ actP1 +,code驴=+ codeASM +,ICT2 =+ ictM2
                +,ActiveP2 =+ actP2 +,ICT3 =+ ictM3 +,ActiveP3 =+ actP3 +];
    }}

after creating an array how can display all the details in that array? here is what i did so far..

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));
				  
		
	}


}
}

this is the Student Class,

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 + "]";
}
}

when program is executed it asks for inputs. but when i input values,this is the output i get,,

Student [fName=, Lname=, studentID=, marksProject1=0, marksICT1=0, ActiveP=0, Code Ass=0, ICT2=0, ActiveP2=0, ICT3=0, ActiveP3=76]

Note that only ActiveP3 value is correctly printed!!

how to correct this? i am new to java.this is my first assignment.

解决方案

Create only one new object in each iteration and use an empty constructor as Eran suggested:

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();
    }

}

Add the empty constructor to your Student class, don't make the variables static and modify the toString() method:

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 + "]";
    }

}

这篇关于如何显示存储的信息在Java中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 20:29