gpt4 book ai didi

java - 数组项目似乎只打印出null

转载 作者:行者123 更新时间:2023-12-01 09:20:17 24 4
gpt4 key购买 nike

程序应该为studentArr 数组中的Student 对象分配随机值。现在它只打印出空值。我尝试过几种不同的打印方法。

我很确定我应该填写每个学生对象属性的方法或我的打印语句有问题。

import java.util.*;
public class Lab6Excercise
{
String[] first={"Rick","Morty","Moriarty","Samus","Promethius","Geiger","Moriarti","Bob","Taco",
"Asparagus","Shoes","Potato","Dirty","Dan","Spongebob","Space","Nova","Illadin","Orange","Electron"};

String[] last={"PoopyButthole","Red","Mantis","Toboggan","Oak","Elm","Dumbledore","Potter","Spice","Toothbrush","Argon",
"Blitz","LazerWolf","Mc-BigMac","King","Queen","Spork","Petrolium","Apple","Trash"};

//no syntax errors if set to static, but prints out null for everything.
//problem here or in calling array to print?

public static Student[] studentArr= new Student[20];
public Lab6Excercise(){
initializeArray();
}

public void initializeArray(){
for(int i=0; i>20; i++){
//this one seems correct but it cant find the symbol method- setStudentID etc. methods
// online it said to try (Student)studentArr.setStudentID(id())
//which explicitely casts it

//seems to work
//http://stackoverflow.com/questions/29328569/setting-values-to-an-array-of-objects
studentArr[i]= new Student();
studentArr[i].setStudentID(id());
studentArr[i].setFirstName(First());
studentArr[i].setLastName(Last());
studentArr[i].setGrade(Grades());

//other way?probably wrong
//studentArr[i]= new Student(studentArr.id(),
//studentArr.First(),
//studentArr.Last(),
//studentArr.Grades());
}
}

public Student[] getStudentArr(){
return studentArr;
}
//do i even need this?
//public Lab6Excercise getLab(){
// return lab;
//}
public static void main(String[] args){
//randomly generate double grade, student id;
//create 2 arrays, first name, last name. pull randomly for name generation

//necessary? how do i
Lab6Excercise lab= new Lab6Excercise();

//prints out hash or something
//System.out.println(lab.studentArr.toString());
//prints out nulls
// System.out.println(Arrays.deepToString(studentArr));

for(int i=0; i<studentArr.length; i++){
System.out.println(studentArr[i]);
}

}
public int id(){
int ID=1+(int)(Math.random()*((100-1)+1));
return ID;
}

public String First(){
Random random= new Random();
int index= random.nextInt(first.length);
return first[index];
}

public String Last(){
Random random= new Random();
int index= random.nextInt(last.length);
return last[index];
}

public double Grades(){
double grade=0+(double)(Math.random()*((4-1)+1));
return grade;
}
}

和对象类

import java.util.Random;
public class Student
{
private int studentID;
private String firstName;
private String lastName;
private double grade;

// no-argument constructor calls other constructor with default values
public Student()
{
this( 0, "", "", 0.0 ); // call four-argument constructor
} // end no-argument Student constructor

// initialize a record
public Student( int id, String first, String last, double grade )
{
setStudentID( id );
setFirstName( first );
setLastName( last );
setGrade( grade );
} // end four-argument Student constructor

// set student ID number
public void setStudentID( int id )
{
studentID = id;
} // end method setStudentID

// get student ID number
public int getStudentID()
{
return studentID;
} // end method getStudentID

// set first name
public void setFirstName( String first )
{
firstName = first;
} // end method setFirstName

// get first name
public String getFirstName()
{
return firstName;
} // end method getFirstName

// set last name
public void setLastName( String last )
{
lastName = last;
} // end method setLastName

// get last name
public String getLastName()
{
return lastName;
} // end method getLastName

// set grade
public void setGrade( double gradeValue )
{
grade = gradeValue;
} // end method setGrade

// get grade
public double getGrade()
{
return grade;
} // end method getGrade

public static int getRandom(int[] array){
int rnd= new Random().nextInt(array.length);
return array[rnd];
}
public String toString(){
return "First name: "+firstName + "Last name: " + lastName+ "ID: "+ studentID+ "Grade: " + grade+"\n";
}
}

最佳答案

您永远不会初始化数组,因为您的 for 循环索引是错误的。

for (int i = 0; i > 20; i++)

这表示:对于从 0 开始的 i,如果 i 大于 20,则运行以下代码,每次将i的值增加1。由于 i 一开始就小于 20,因此它永远不会大于 20,并且代码永远不会运行。相反,请执行以下操作,使运行条件 i 小于 20:

for (int i = 0; i < 20; i++)

或者,更好的是:

for (int i = 0; i < studentArr.length; i++)
<小时/>

有关 for 语句语法的更多详细信息,请查看 Java documentation .

关于java - 数组项目似乎只打印出null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40208988/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com