我在搜索数组中遇到问题; 以下是参数: 该方法允许学生从他们现有的时间表中搜索课程名称。 使用WHILE循环,当两个条件之一为真时将停止:要么找到课程,要么已到达计划中的最后一个课程并且没有匹配。 如果找到课程,则打印出课程名称和学分数。 如果找不到该课程,则打印出一条消息,说明该课程未列入学生的课程表。 只搜索现有课程。如果数组未满,并且您搜索整个数组,则程序将出现运行时错误。
到目前为止,这是我的代码,提前谢谢你:
public class Student {
//Instance Data
String studentName;
String studentID;
String streetAddress;
String city;
String state;
String zipCode;
String major;
int totalCredits;
final int MAX_CREDITS = 18;
final int SIZE = 6;
String[] schedule = new String[SIZE];
int courseNumber = 0;
//Create Constructor:
//Initializes the student data at instantiation time.
//-------------------------------------------------------
// Sets up the student's information.
//-------------------------------------------------------
public Student(String name, String id, String address, String cityName, String stateName, String zip, String area) {
studentName = name;
studentID = id;
streetAddress = address;
city = cityName;
state = stateName;
zipCode = zip;
major = area;
} //end Student Constructor
//Method to Return student information as string:
//-------------------------------------------------------
// Returns the student information as a formatted string.
//-------------------------------------------------------
public String toString() {
String studentInfo;
studentInfo = "Name:\t\t\t" + studentName + "\n" + "ID:\t\t\t" + studentID + "\n" + "Address:\t\t" + streetAddress + "\n" + "City:\t\t\t" + city + "\n" + "State:\t\t\t" + state + "\n" + "Zip Code:\t\t" + zipCode + "\n" + "Major:\t\t\t" + major + "\n";
return studentInfo;
} // end toString
//Method to determine if maximum allowed credits have been exceeded
//-------------------------------------------------------
// Returns true if total credits does not exceed 18.
//-------------------------------------------------------
private boolean checkCredits(int numCredits) {
if (numCredits + totalCredits <= MAX_CREDITS) //make sure max credits not exceeded
{
return true; //return a true if still less than 18 credits
} else {
return false; //return a false if 18 credit limit is exceeded
} //end numCredits
} //checkCredits
//Method to add a course to the student’s schedule
//-------------------------------------------------------
// Adds a course to the array if total credits does not exceed 18.
//-------------------------------------------------------
public void addCourse(String course, int numCredits) {
if (courseNumber < SIZE) //make sure array is not full.
{
if (checkCredits(numCredits) == true) //if we’re under 18 credits
{
//add course
schedule[courseNumber] = course + ":\t\t" + numCredits + "\tCredits\n";
//increment number of credits
totalCredits = totalCredits + numCredits;
//increment number of courses
courseNumber = courseNumber + 1;
} else //oops – can’t do more than 18 credits
{
System.out.println("You have exceeded the maximum allowed credits.");
} //end checkCredits
} else //oops – can’t do more than 10 courses
{
System.out.println("You have exceeded 10 courses.");
} //end courseNumber
} //addCourse
//Method to display the schedule
//-------------------------------------------------------
// Will only print out the courses added to the array.
//-------------------------------------------------------
public void displaySchedule() {
for (int index = 0; index < courseNumber; index++) {
System.out.println("Course #" + (index + 1) + " " + schedule[index] + "\n");
} //end for
} //end display schedule
public void searchCourse(String courseName) {
int index;
String extract;
boolean notFound = true;
index = 0;
while (index < SIZE) {
extract = schedule(index).charAt(0, 6);
if (courseName != extract) {
index++;
} else {
notFound = false;
}
if (notFound = true) {
System.out.println("Course Found");
} else {
System.out.println("Course Not Found");
}
}
}
}
答案 0 :(得分:0)
搜索方法看起来效率不高,可能是这样的:
while(index++ < SIZE){
extract = schedule(index).charAt(0,6);
if(!(extract == courseName)){ // i belive this returns 0 if true, which would make our if statement false, just use the not operator
// found the course, do stuff
return;
}
}
// if loop ends, then we didn't find the course, do stuff