我作为一个Java新手遇到了困难(也是在编程方面),我们被分配了一个任务。该任务分为三个部分,检查给定字符串是否具有平衡的括号。
"规则"如下:
- "abcdefksdhgs" - 平衡的 - "[{aaa<bb>dd}]<232>" - 平衡的 - "[ff{<gg}]<ttt>" - 不平衡('<'没有关闭) - "{<}>" - 不平衡
任务的第一部分是编写一个方法,该方法将获取包含字符串的字符数组,并找到第一个包含以下括号的索引(=数组单元格):
当然,这很容易实现:
第三部分是编写一个递归方法,该方法将获取一个字符串,并且仅当该字符串为平衡括号字符串时返回“true”。当然,我们需要使用我们编写的第一和第二个方法,并且还给出了一个提示:
提示:使用一个辅助方法,该方法将获取2个字符串。
在这一部分中,我被卡住了。我想出了几个停止情况:
1.如果给定的字符串中根本没有括号-则返回true 2.如果给定的字符串为空,则返回true(此选项在第一个方法中已经覆盖) 3.如果找到开放括号和匹配的闭合括号-则返回true 否则,返回false。 在代码编写方面,我目前被卡住了,不知道如何从第26行的递归调用继续编写此方法的代码:
"规则"如下:
- "abcdefksdhgs" - 平衡的 - "[{aaa<bb>dd}]<232>" - 平衡的 - "[ff{<gg}]<ttt>" - 不平衡('<'没有关闭) - "{<}>" - 不平衡
任务的第一部分是编写一个方法,该方法将获取包含字符串的字符数组,并找到第一个包含以下括号的索引(=数组单元格):
} , { , ] , [ , ( , ) , > , <
当然,这很容易实现:
/**
* bracketIndex - 1st Method:
* this method will get a single string (read from the text file),
* and will find the first index char that is any bracket of the following: },{,],[,(,),>,<
* @param str1 - the given string.
* @return index - the first index that contains character that is one of the brackets listed above.
*/
public static int bracketIndex(String str1){
int index = -1; // default value: didn't find any bracket in the string.
int i = 0;
for( i = 0; i < str1.length(); i++ ){
if(str1.charAt(i) == '}' || str1.charAt(i) == '{' || str1.charAt(i) == ']' || str1.charAt(i) == '[' || str1.charAt(i) == '(' || str1.charAt(i) == ')' || str1.charAt(i) == '>' || str1.charAt(i) == '<'){
return index = i;
}//if
}//for
return index;
}//end of bracketIndex
第二部分的任务是编写一个方法,该方法将获取两个字符,并仅在第二个字符是第一个字符的适当闭合括号时返回 true(例如:1st='<' 2nd='>' = true(相反为 false!),1st='<' 2nd='e' = false)。这也很容易:
/**
* checkBracket - 2nd Method:
*
* @param firstChar, secondChar - two chars.
* @return True - if the the two chars are brackets, in which the second char is the closing bracket of the first char
*/
public static boolean checkBracket(char firstChar, char secondChar){
if ( (firstChar == '(') && (secondChar == ')') ||
(firstChar == '[') && (secondChar == ']') ||
(firstChar == '{') && (secondChar == '}') ||
(firstChar == '<') && (secondChar == '>') ){
return true;
}//if
return false;
}//end of checkBracket
第三部分是编写一个递归方法,该方法将获取一个字符串,并且仅当该字符串为平衡括号字符串时返回“true”。当然,我们需要使用我们编写的第一和第二个方法,并且还给出了一个提示:
提示:使用一个辅助方法,该方法将获取2个字符串。
在这一部分中,我被卡住了。我想出了几个停止情况:
1.如果给定的字符串中根本没有括号-则返回true 2.如果给定的字符串为空,则返回true(此选项在第一个方法中已经覆盖) 3.如果找到开放括号和匹配的闭合括号-则返回true 否则,返回false。 在代码编写方面,我目前被卡住了,不知道如何从第26行的递归调用继续编写此方法的代码:
/**
* checkBalance - 3rd Method:
* will check if a given string is a balanced string.
* @param str1 - the given string to check.
* @return true - if the given string is balanced, false - if the given string isn't balanced.
*/
public static boolean checkBalance(String str1){
boolean ans;
int a = bracketIndex(str1);
if ( a == -1 ){
return ans = true;
}//if
if( str1.charAt(a) == '{' ||
str1.charAt(a) == '[' ||
str1.charAt(a) == '<' ||
str1.charAt(a) == '(' ){
int b = bracketIndex(str1.substring(a))+1 ;
if( b != 0 ){
if( checkBracket ( str1.charAt(a), str1.charAt(b) ) == true ){
return ans = true;
}//if
if( str1.charAt(b) == '{' ||
str1.charAt(b) == '[' ||
str1.charAt(b) == '<' ||
str1.charAt(b) == '(' ){
checkBalance(str1.substring(b-1));
}//if
else{
return ans = false;
}//else
}//if
}//if
return ans = false;
}//end of checkBalance
如果第26行的递归代码返回true,我不知道该如何继续。
我很高兴能从这里的专家那里得到一些指导,告诉我应该朝哪个方向前进,或者我从一开始就做错了。