Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java需要帮助才能打印所有字符串_Java_Methods - Fatal编程技术网

Java需要帮助才能打印所有字符串

Java需要帮助才能打印所有字符串,java,methods,Java,Methods,伙计们,我需要这个代码的帮助 我需要按命令打印所有字符串(PrintAllStudents)我做了这个命令,但我没有让它打印所有学生它只打印最近一个学生添加的im学习java 这是我的测试课: package com.tj; public class Student { private int StudentId; private String StudentName; public void AddStudent(int StudentId, String Stu

伙计们,我需要这个代码的帮助 我需要按命令打印所有字符串(PrintAllStudents)我做了这个命令,但我没有让它打印所有学生它只打印最近一个学生添加的im学习java 这是我的测试课:

package com.tj;

public class Student {

    private int StudentId;
    private String StudentName;

    public void AddStudent(int StudentId, String StudentName){
    this.StudentId = StudentId;
    this.StudentName = StudentName;
    }
    public void PrintAllStudents() {
        System.out.println("Id : " + StudentId);
        System.out.println("StudentName : " + StudentName);

    }
}
我的测试班:

package com.tj;

public class Test {
    public static void main(String[] args) {
        Student s = new Student();
        s.AddStudent(1, "Walied");
        s.AddStudent(2, "Ayman");
        s.AddStudent(3, "Mostafa");
        s.PrintAllStudents();
    }

}

当您调用
AddStudent
时,您将替换上一个学生。
您可以创建
List List=new ArrayList()代替。
然后添加
list.add(s)哪里

Student s=新学生()

当您调用
AddStudent
时,您将替换上一个学生。
您可以创建
List List=new ArrayList()代替。
然后添加
list.add(s)哪里

Student s=新学生()

当您调用
AddStudent
时,您将替换上一个学生。
您可以创建
List List=new ArrayList()代替。
然后添加
list.add(s)哪里

Student s=新学生()

当您调用
AddStudent
时,您将替换上一个学生。
您可以创建
List List=new ArrayList()代替。
然后添加
list.add(s)哪里

Student s=新学生()

您只创建了一个Student类型的对象。你给它分配了三个不同的值。因此,前两个值将被覆盖。如果有三个学生,则需要创建三个学生对象

    Student s = new Student();   //Creates a new Student object.
    s.AddStudent(1, "Walied");   //This will assign 1 and Walied as ID and name to the objcet.
    s.AddStudent(2, "Ayman");    // Now again, you are assigning new ID and name to the same object. So the previous value is overwritten.
    s.AddStudent(3, "Mostafa");  // Same effect as above. 
因此,只有最后一个值可供您使用

您可以创建三个Student类型的对象,并为每个对象指定以下三个值

Student s1 = new Student();
Student s2 = new Student();
Student s3 = new Student();
s1.AddStudent(1, "Walied");
s2.AddStudent(2, "Ayman");
s3.AddStudent(3, "Mostafa");
 Student s = new Student();
 s.AddStudent(1, "Walied");
并使用每个对象的打印方法打印其值

您可以创建Student类型的列表,并在每次使用以下值填充对象时将其添加到该列表中

Student s1 = new Student();
Student s2 = new Student();
Student s3 = new Student();
s1.AddStudent(1, "Walied");
s2.AddStudent(2, "Ayman");
s3.AddStudent(3, "Mostafa");
 Student s = new Student();
 s.AddStudent(1, "Walied");

现在将其添加到Student类型的列表中,最后,遍历该列表并为每个对象调用print方法

您只创建了Student类型的一个对象。你给它分配了三个不同的值。因此,前两个值将被覆盖。如果有三个学生,则需要创建三个学生对象

    Student s = new Student();   //Creates a new Student object.
    s.AddStudent(1, "Walied");   //This will assign 1 and Walied as ID and name to the objcet.
    s.AddStudent(2, "Ayman");    // Now again, you are assigning new ID and name to the same object. So the previous value is overwritten.
    s.AddStudent(3, "Mostafa");  // Same effect as above. 
因此,只有最后一个值可供您使用

您可以创建三个Student类型的对象,并为每个对象指定以下三个值

Student s1 = new Student();
Student s2 = new Student();
Student s3 = new Student();
s1.AddStudent(1, "Walied");
s2.AddStudent(2, "Ayman");
s3.AddStudent(3, "Mostafa");
 Student s = new Student();
 s.AddStudent(1, "Walied");
并使用每个对象的打印方法打印其值

您可以创建Student类型的列表,并在每次使用以下值填充对象时将其添加到该列表中

Student s1 = new Student();
Student s2 = new Student();
Student s3 = new Student();
s1.AddStudent(1, "Walied");
s2.AddStudent(2, "Ayman");
s3.AddStudent(3, "Mostafa");
 Student s = new Student();
 s.AddStudent(1, "Walied");

现在将其添加到Student类型的列表中,最后,遍历该列表并为每个对象调用print方法

您只创建了Student类型的一个对象。你给它分配了三个不同的值。因此,前两个值将被覆盖。如果有三个学生,则需要创建三个学生对象

    Student s = new Student();   //Creates a new Student object.
    s.AddStudent(1, "Walied");   //This will assign 1 and Walied as ID and name to the objcet.
    s.AddStudent(2, "Ayman");    // Now again, you are assigning new ID and name to the same object. So the previous value is overwritten.
    s.AddStudent(3, "Mostafa");  // Same effect as above. 
因此,只有最后一个值可供您使用

您可以创建三个Student类型的对象,并为每个对象指定以下三个值

Student s1 = new Student();
Student s2 = new Student();
Student s3 = new Student();
s1.AddStudent(1, "Walied");
s2.AddStudent(2, "Ayman");
s3.AddStudent(3, "Mostafa");
 Student s = new Student();
 s.AddStudent(1, "Walied");
并使用每个对象的打印方法打印其值

您可以创建Student类型的列表,并在每次使用以下值填充对象时将其添加到该列表中

Student s1 = new Student();
Student s2 = new Student();
Student s3 = new Student();
s1.AddStudent(1, "Walied");
s2.AddStudent(2, "Ayman");
s3.AddStudent(3, "Mostafa");
 Student s = new Student();
 s.AddStudent(1, "Walied");

现在将其添加到Student类型的列表中,最后,遍历该列表并为每个对象调用print方法

您只创建了Student类型的一个对象。你给它分配了三个不同的值。因此,前两个值将被覆盖。如果有三个学生,则需要创建三个学生对象

    Student s = new Student();   //Creates a new Student object.
    s.AddStudent(1, "Walied");   //This will assign 1 and Walied as ID and name to the objcet.
    s.AddStudent(2, "Ayman");    // Now again, you are assigning new ID and name to the same object. So the previous value is overwritten.
    s.AddStudent(3, "Mostafa");  // Same effect as above. 
因此,只有最后一个值可供您使用

您可以创建三个Student类型的对象,并为每个对象指定以下三个值

Student s1 = new Student();
Student s2 = new Student();
Student s3 = new Student();
s1.AddStudent(1, "Walied");
s2.AddStudent(2, "Ayman");
s3.AddStudent(3, "Mostafa");
 Student s = new Student();
 s.AddStudent(1, "Walied");
并使用每个对象的打印方法打印其值

您可以创建Student类型的列表,并在每次使用以下值填充对象时将其添加到该列表中

Student s1 = new Student();
Student s2 = new Student();
Student s3 = new Student();
s1.AddStudent(1, "Walied");
s2.AddStudent(2, "Ayman");
s3.AddStudent(3, "Mostafa");
 Student s = new Student();
 s.AddStudent(1, "Walied");

现在将其添加到Student类型的列表中,最后,遍历该列表并为每个对象调用print方法

您总是使用添加Student的调用覆盖学生信息。如果您只有一个ID和一个名称,并且始终将其设置为新值,则会替换以前的信息


您也不应该将addStudent方法指定给Student对象,因为它与单个Student无关,而是与Student集合相关。您可以创建一个包含学生对象集合的类,并将其放置在那里。然后在addStudent()中创建一个新的Student对象并将其放入集合。

您总是通过调用add Student来覆盖学生信息。如果您只有一个ID和一个名称,并且始终将其设置为新值,则会替换以前的信息


您也不应该将addStudent方法指定给Student对象,因为它与单个Student无关,而是与Student集合相关。您可以创建一个包含学生对象集合的类,并将其放置在那里。然后在addStudent()中创建一个新的Student对象并将其放入集合。

您总是通过调用add Student来覆盖学生信息。如果您只有一个ID和一个名称,并且始终将其设置为新值,则会替换以前的信息


您也不应该将addStudent方法指定给Student对象,因为它与单个Student无关,而是与Student集合相关。您可以创建一个包含学生对象集合的类,并将其放置在那里。然后在addStudent()中创建一个新的Student对象并将其放入集合。

您总是通过调用add Student来覆盖学生信息。如果您只有一个ID和一个名称,并且始终将其设置为新值,则会替换以前的信息

您也不应该将addStudent方法分配给Student对象,因为它与单个Student对象无关