Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.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_Arrays - Fatal编程技术网

Java 以数组形式打印学生

Java 以数组形式打印学生,java,arrays,Java,Arrays,我试图在数组中输入学生的姓和名,以便调用学生信息并在成绩册中使用 Student类有一个名为Student的方法(字符串last\u name,字符串first\u name) 我不知道如何将学生打印在如下列表中: last name, first name last name, first name 以下是我目前的计划: public static void main (String[] args) { System.out.println("--------------------

我试图在数组中输入学生的姓和名,以便调用学生信息并在成绩册中使用

Student
类有一个名为
Student的方法(字符串last\u name,字符串first\u name)

我不知道如何将学生打印在如下列表中:

last name, first name
last name, first name
以下是我目前的计划:

public static void main (String[] args)
{
    System.out.println("--------------------------------");
    System.out.println("Welcome to the Gradebook Program");
    System.out.println("--------------------------------");
    System.out.println();

    students = GetNumberOfStudents();

    //Allocate space for student information
    Student student[] = new Student[students];

    for (int i = 0; i < students; i++)
    {
        System.out.println("Student #" + (i+1));

        System.out.print("\tEnter last name: ");
        student[i] = scan.nextLine();
        System.out.print("\tEnter first name: ");
        student[i] = scan.nextLine();
    }

    System.out.println(student[i]);
publicstaticvoidmain(字符串[]args)
{
System.out.println(“-------------------------------------”);
System.out.println(“欢迎参加成绩册计划”);
System.out.println(“-------------------------------------”);
System.out.println();
学生=GetNumberOfStudents();
//为学生信息分配空间
学生[]=新生[学生];
for(int i=0;i
我希望我们需要看到Student的定义,您已经给出了构造函数,但没有给出getter/setter

我希望打印代码看起来像

for (Student s : student) {
    System.out.println(s.getLastName() + "," + s.getFirstName());
}
您也没有正确初始化学生对象。 在你写的循环中,我希望看到

new Student(lastname, firstname);

这是一个学生课堂的练习,就像你描述的那样

package test; //change to your package name

import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class Student{

    private String m_LastName;
    private String m_FirstName;
    private int[] TestScores = new int[3];

    //Constructor
    public Student(String last, String first) {
        m_LastName = last;
        m_FirstName = first;
    }

    //returns firstname
    public String firstName() {
        return m_FirstName;
    }

    //returns lastname
    public String lastName() {
        return m_LastName;
    }

    //set a test score at a given index
    public void setTestScore(int index, int score) {
        TestScores[index] = score;
    }

    //returns test score at a given index
    public int testScore(int index) {
        return TestScores[index];
    }

    //returns testscores average
    public double testAverage() {
        int sum = 0;
        for(int i = 0; i<TestScores.length; i++) {
            sum += TestScores[i];
        }
        return sum/TestScores.length;
    }

    //returns students highest test score
    public int maxTestScore() {
        //sort the array
        for(int i = 0; i<TestScores.length; i++) {
            for(int j = 0; j<TestScores.length; j++) {
                if(TestScores[i]<TestScores[j]) {
                    int buffer;
                    buffer = TestScores[i];
                    TestScores[i] = TestScores[j];
                    TestScores[j] = buffer;
                }   
            }
        }
        return TestScores[TestScores.length-1];
    }


    public boolean isPassing() {
        //TODO: when hes passing?
        return true;
    }




    public static void main (String[] args)
    {
        Scanner scan = new Scanner(new InputStreamReader(System.in));

        System.out.println("--------------------------------");
        System.out.println("Welcome to the Gradebook Program");
        System.out.println("--------------------------------");
        System.out.println();

        /**
         * dont know where you declare the students variable
         * and how the getnumberofstudents function looks like
         * 
         * students = GetNumberOfStudents();  
         */  
        int students = 1;

        List<Student> StudentList = new ArrayList<>();   //creat a list which can store student objects

        for (int i = 0; i < students; i++)
        {

            System.out.println("Student #" + (i+1));

            System.out.print("\tEnter last name: ");
            String lastname = scan.nextLine();        //store lastname in a variable 
            System.out.print("\tEnter first name: ");
            String firstname = scan.nextLine();       //store firstname in a variable 

            Student student = new Student(lastname, firstname);  //creat new student object with the last and firstname
            StudentList.add(student);                            //add it to the student list
        }


        //print out all students first and lastnames. here you can add the other data you want to print.
        for (int i = 0; i < students; i++)
        {
            System.out.println("List of all Students:");
            System.out.println("Firstname:"+StudentList.get(i).firstName()+" Lastname:"+StudentList.get(i).lastName());
        }
    }

}
包测试;//更改包名
导入java.io.InputStreamReader;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.Scanner;
公立班学生{
私有字符串m_LastName;
私有字符串m_FirstName;
私有整数[]测试分数=新整数[3];
//建造师
公立学生(最后一个字符串,第一个字符串){
m_LastName=last;
m_FirstName=第一;
}
//返回名字
公共字符串firstName(){
返回m_FirstName;
}
//返回lastname
公共字符串lastName(){
返回m_LastName;
}
//在给定的索引上设置测试分数
公共无效设置分数(整数索引,整数分数){
测试分数[指数]=分数;
}
//返回给定索引处的测试分数
公共整数测试分数(整数索引){
返回测试分数[索引];
}
//返回测试分数平均值
公共双重测试平均值(){
整数和=0;

对于(int i=0;i您认为这有什么作用:
student[i]=scan.nextLine();
?当您执行两次时会发生什么?您不能在循环之外访问i。您所做的只是获取信息,但您没有创建新的
student
对象。这甚至不会编译。
student[]
是一个
学生
对象数组,
扫描.nextLine()
返回一个
字符串
。请也显示学生类。所有这些
学生
家庭作业…:)我只是有这个错误…..java:24:错误:找不到符号列表StudentList=new ArrayList();这是我老师给出的一个例子,演示如何从学生课堂上获取信息……//演示构造器student1=新学生();student2=新学生(“Smith”,“Terry”);//为每个学生存储3个测试分数1.设置分数(0,85);student1.设置分数(1,90);student1.设置分数(2,78);student2.setTestScore(0,79);student2.setTestScore(1,98);student2.setTestScore(2,91);//显示每个学生对象System.out.println(“学生1:+student1”);System.out.println(“学生2:+student2”);无法找到符号出现错误是因为编译器找不到代码的某一部分。例如,学生类。是否导入了学生类和List+ArrayList?请按Strg+Shift+O来组织导入。我使用我编写的学生类添加了整个代码。它看起来像您描述的学生类。此代码有效。