由于我是初学者,我在做一些功课方面遇到了一些麻烦。我简化了代码,尝试对较小的部分进行故障排除。我不能打印任何东西。
我很确定这源于addStudent
方法中的问题。我认为它不是打印,因为if (stud != null)
条件返回true并且不允许它到达实际添加学生的if (numStudents < MAX_STUDENTS)
。
我无法摆脱if (stud != null)
或更改条件所在的顺序。如果我这样做,那么由于螺柱指向任何东西,我得到nullPointerException。
请帮忙。我不知道还有什么可以尝试。
这是简化的代码。 (对于缩进感到抱歉,我不知道如何把它放到这个很好的位置)
public class Foothill
{
public static void main (String[] args)
{
int k;
//set up even group of new students
Student [] myClass={
new Student ("Calvo", "Emmet", 125),
new Student ("Yamaguchi", "Carlin", 343),
new Student ("Ung", "Cari", 42),
new Student ("Tweedy", "Jasper", 202),
new Student ("Lorenz", "3rma", 912),
new Student ("0Rlando", "Brandee", 50),
new Student ("Swindell", "Mavis", 936),
new Student ("Sugibayashi", "Nora", 231),
new Student ("Ching", "Gavin", 269),
};
// instantiate a StudArrUtilObject
StudentArrayUtilities myStuds = new StudentArrayUtilities();
// we can add students manually and individually
myStuds.addStudent( new Student("bartman", "petra", 102));
myStuds.addStudent( new Student("charters", "rodney", 295));
// if we happen to have an array available, we can add students in loop.
for (k = 0; k < myClass.length; k++)
myStuds.addStudent( myClass[k] );
System.out.println(myStuds.toString("Before:"));
}
}
class Student
{
//declare members
private String firstName;
private String lastName;
private int totalPoints;
public static final String DEFAULT_NAME = "zz-error";
public static final int DEFAULT_POINTS = 0;
public static final int MAX_POINTS = 1000;
// constructor requires parameters - no default supplied
public Student(String last, String first, int points)
{
if ( !setLastName(last) )
lastName = DEFAULT_NAME;
if ( !setFirstName(first) )
firstName = DEFAULT_NAME;
if ( !setPoints(points) )
totalPoints = DEFAULT_POINTS;
}
//accessor methods
public String getLastName()
{
return lastName;
}
public String getFirstName()
{
return firstName;
}
public int getTotalPoints()
{
return totalPoints;
}
//mutator methods
boolean setLastName(String last)
{
if ( !validString(last) )
return false;
lastName = last;
return true;
}
boolean setFirstName(String first)
{
if ( !validString(first) )
return false;
firstName = first;
return true;
}
boolean setPoints(int pts)
{
if ( !validPoints(pts) )
return false;
totalPoints = pts;
return true;
}
//tells caller what to print
public String toString()
{
String resultString;
resultString = " "+ lastName
+ ", " + firstName
+ " points: " + totalPoints
+ "\n";
return resultString;
}
//checks for valid inputs
private boolean validString( String testStr )
{
if (testStr != null && Character.isLetter(testStr.charAt(0)))
return true;
return false;
}
private boolean validPoints( int testPoints )
{
if (testPoints >= 0 && testPoints <= MAX_POINTS)
return true;
return false;
}
}
class StudentArrayUtilities
{
//members
public int numStudents;
public Student [] theArray;
final int MAX_STUDENTS = 20;
// print the array with string
public String toString(String title)
{
String output = "";
for (int k = 0; k < numStudents; k++)
output += "title"+ theArray[k].toString();
return output;
}
public boolean addStudent( Student stud )
{
if (stud != null)
return true;
if (numStudents < MAX_STUDENTS)
{
theArray[numStudents] = stud;
numStudents++;
return true;
}
else
return false;
}
}
答案 0 :(得分:0)
这是错误的:
public boolean addStudent(Student stud) {
if (stud != null)
return true;
if (numStudents < MAX_STUDENTS) {
theArray[numStudents] = stud;
numStudents++;
return true;
} else
return false;
}
特别是:
if (stud != null)
return true;
导致您的方法在每次学生参数与null不同时返回true
,最后,该条件是原因:
numStudents
永远不会增加
以及为什么
此代码
for (int k = 0; k < numStudents; k++)
output += "title" + theArray[k].toString();
与
相同 for (int k = 0; k < 0; k++)
output += "title" + theArray[k].toString();
麦角:
output
永远不会更改其初始值""
(空字符串),这就是您最后打印的内容(实际上是打印,但打印空字符串)...
答案 1 :(得分:0)
我已经在StudentArrayUtilities类中添加了一个构造函数,以初始化数组和numStudents变量:
public boolean addStudent( Student stud )
{
if (stud == null)
return false;
if (numStudents < MAX_STUDENTS)
{
theArray[numStudents] = stud;
numStudents++;
return true;
}
return false;
}
此外,我已经编辑了你的addStudent方法,因为如果stud!= null,则返回true是错误的:
SELECT *
FROM TABLE
WHERE DATE_COLUMN >= (SELECT CASE WHEN TD_DAY_OF_WEEK(CURRENT_DATE) = 2
THEN CURRENT_DATE - 3
ELSE CURRENT_DATE - 1
END)
AND DATE_COLUMN <= CURRENT_DATE;
输出后:
标题巴特曼,佩特拉积分:102个冠军章程,罗德尼点数:295 标题卡尔沃,埃米特得分:125冠军山口,卡林积分:343 冠军Ung,Cari积分:42冠军Tweedy,贾斯帕积分:202冠军 Lorenz,zz-error points:912 title zz-error,Brandee积分:50分 Swindell,Mavis积分:936冠军Sugibayashi,Nora积分:231冠军 Ching,Gavin指出:269
答案 2 :(得分:0)
我认为你的方法应该是这样的:
public boolean addStudent(Student stud) {
if (stud != null && numStudents < MAX_STUDENTS) {
theArray[numStudents] = stud;
numStudents++;
return true;
} else {
return false;
}
}
您也可以跳过维护numStudents并尝试使用theArray.lenght。