我不明白为什么我的程序在 运行 时不打印
I can't figure out why my program isn't printing when I run it
作为初学者,我在做一些家庭作业时遇到了一些麻烦。我简化了代码以尝试对较小的部分进行故障排除。我无法打印任何东西。
我很确定这源于 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;
}
}
这是错误的:
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;
这导致您的方法 returns true
每次您的学生参数不同于 null,最后,该条件是原因:
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
永远不会改变它的初始值 ""
(空字符串),这就是你在最后打印的内容(你实际上是在打印,但是打印一个空字符串)...
我已将构造函数添加到您的 StudentArrayUtilities class 以初始化数组和 numStudents 变量:
public StudentArrayUtilities() {
numStudents = 0;
theArray = new Student[MAX_STUDENTS];
}
此外,我已经编辑了您的 addStudent 方法,因为 return true if stud != null:
是错误的
public boolean addStudent( Student stud )
{
if (stud == null)
return false;
if (numStudents < MAX_STUDENTS)
{
theArray[numStudents] = stud;
numStudents++;
return true;
}
return false;
}
此输出后:
title bartman, petra points: 102 title charters, rodney points: 295
title Calvo, Emmet points: 125 title Yamaguchi, Carlin points: 343
title Ung, Cari points: 42 title Tweedy, Jasper points: 202 title
Lorenz, zz-error points: 912 title zz-error, Brandee points: 50 title
Swindell, Mavis points: 936 title Sugibayashi, Nora points: 231 title
Ching, Gavin points: 269
我认为你的方法应该是这样的:
public boolean addStudent(Student stud) {
if (stud != null && numStudents < MAX_STUDENTS) {
theArray[numStudents] = stud;
numStudents++;
return true;
} else {
return false;
}
}
您也可以跳过维护 numStudents 并尝试使用 theArray.lenght。
作为初学者,我在做一些家庭作业时遇到了一些麻烦。我简化了代码以尝试对较小的部分进行故障排除。我无法打印任何东西。
我很确定这源于 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;
}
}
这是错误的:
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;
这导致您的方法 returns true
每次您的学生参数不同于 null,最后,该条件是原因:
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
永远不会改变它的初始值 ""
(空字符串),这就是你在最后打印的内容(你实际上是在打印,但是打印一个空字符串)...
我已将构造函数添加到您的 StudentArrayUtilities class 以初始化数组和 numStudents 变量:
public StudentArrayUtilities() {
numStudents = 0;
theArray = new Student[MAX_STUDENTS];
}
此外,我已经编辑了您的 addStudent 方法,因为 return true if stud != null:
是错误的public boolean addStudent( Student stud )
{
if (stud == null)
return false;
if (numStudents < MAX_STUDENTS)
{
theArray[numStudents] = stud;
numStudents++;
return true;
}
return false;
}
此输出后:
title bartman, petra points: 102 title charters, rodney points: 295 title Calvo, Emmet points: 125 title Yamaguchi, Carlin points: 343 title Ung, Cari points: 42 title Tweedy, Jasper points: 202 title Lorenz, zz-error points: 912 title zz-error, Brandee points: 50 title Swindell, Mavis points: 936 title Sugibayashi, Nora points: 231 title Ching, Gavin points: 269
我认为你的方法应该是这样的:
public boolean addStudent(Student stud) {
if (stud != null && numStudents < MAX_STUDENTS) {
theArray[numStudents] = stud;
numStudents++;
return true;
} else {
return false;
}
}
您也可以跳过维护 numStudents 并尝试使用 theArray.lenght。