Java 检查以点(';';';)结尾的每个句子中的括号是否平衡
我正在为源代码中的测试平衡括号编写java代码 下面是检查平衡圆括号的代码。 大体上 我的职能Java 检查以点(';';';)结尾的每个句子中的括号是否平衡,java,Java,我正在为源代码中的测试平衡括号编写java代码 下面是检查平衡圆括号的代码。 大体上 我的职能 public static boolean process(String s) { Stack<Character> stack = new Stack<Character>(); for(int i = 0; i < s.length(); i++) { char c = s.charAt(i); if(c == '['
public static boolean process(String s) {
Stack<Character> stack = new Stack<Character>();
for(int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if(c == '[' || c == '(' || c == '{' ) {
stack.push(c);
} else if(c == ']') {
if(stack.isEmpty() || stack.pop() != '[') {
System.out.println("yes");
return false;
}
} else if(c == ')') {
if(stack.isEmpty() || stack.pop() != '(') {
return false;
}
} else if(c == '}') {
if(stack.isEmpty() || stack.pop() != '{') {
return false;
}
}
}
return stack.isEmpty();
}
现在我遇到了一个问题,如何检查最后一个索引为点('.')的每个句子中的平衡括号
比如说
输入
输出:
true
true
false
false
false
true
true
你的外部循环不应该是直线而是句子。也就是说,将所有内容读到底,并将其作为
过程的输入。重复直到结束
您可以像这样逐字阅读谢谢您的帮助。但我的输入只是从键盘输入,而不是从文件输入。从哪里读取并不重要System.in
是InputStream
,您可以使用与我链接的答案中的FileInputStream
相同的方法。
input1 : abctex()[]
output: true
So when I die (the [first] I will see in (heaven) is a score list).
[ first in ] ( first out ).
Half Moon tonight (At least it is better than no Moon at all].
A rope may form )( a trail in a maze.
Help( I[m being held prisoner in a fortune cookie factory)].
([ (([( [ ] ) ( ) (( ))] )) ]).
.
.
true
true
false
false
false
true
true