【发布时间】:2016-08-31 19:13:10
【问题描述】:
这是算法,但我在破译过程中遇到了困难 :
set flag equal to false
set index equal to 0
WHILE(index is less than number of courses in array AND flag == false)
extract substring
IF(string1.equals(string2) == true) THEN //course found
set flag to true
ELSE
increment index
END IF
END WHILE
IF flag == false THEN
display message the course name was not found
ELSE
course name found at position index
END IF
这是我的代码:
public void searchCourse(String courseName)
{
int index;
String extract;
boolean notFound = false;
index = 0;
while(index < SIZE)
{
extract = schedule[index].charAt(0,6);
if (courseName == extract){
}
else {
index ++;
}
if ( notFound == false){
System.out.println("Course Not Found");
}
else{
System.out.println("Course Found at: " + index);
}
}
}
【问题讨论】:
-
你在哪里更新 notFound 值?在您的代码中,它始终设置为 false 并且永远不会更改。
-
schedule是String[]吗?charAt没有采用 2 个整数的重载。 -
是的 schedule 是一个 String[] 所以我应该使用 .substring(0,6) 吗? @Jorn Vernee
-
@Cuse 是的,我认为这会满足你的要求。
标签: java arrays string substring