2016-05-05 33 views
-5

我在搜索课程数组时遇到问题; 以下是参数: 该方法将允许学生从现有时间表中搜索课程名称。 使用一个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

这里是一个干净的要点:HTTPS ://gist.github.com/jeorfevre/dd435d610654301dbdfb48c7448313b8 – jeorfevre

+2

那真正的问题是什么?你只是抛弃了作业和大量的代码,没有具体的问题或问题。我们不会为你完成/完成作业。请阅读http://stackoverflow.com/help/mcve和http://stackoverflow.com/help/how-to-ask – tnw

+0

没有人要求任何人做任何事情,只是提供一个洞见@tnw所以放松在那里萌芽。该问题在searchCourse方法试图执行的参数内清楚地陈述。我遇到了使用子字符串从数组计划中提取课程名称的问题 – kidbone222

回答

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