我正在尝试编写一个程序,它将利用堆栈来检查是否存在 左括号/括号在用户的输入中具有重合的右括号/括号。虽然我已经大力搜索了代码,但我似乎无法找到我做错的事情。以下代码块是方法"检查"我试图实施。
import java.util.Stack;
public class Matching {
public static int checking(String s) {
Stack<String> stack = new Stack<>();
for (int i = 0; i < s.length(); i++) {
if (s.substring(i, i + 1).equals("{") || s.substring(i, i + 1).equals("}")){ //if substring is a bracket
if (s.substring(i, i + 1).equals("{")) //if is left bracket push onto stack
stack.push("{");
else if (!stack.peek().equals("{") || stack.size() <= 0) //otherwise, check if left bracket is not at top or if stack is empty
return 3; // "right brace does not have its matching left brace"
else if (s.substring(i, i + 1).equals("}") && stack.peek().equals("{"))//otherwise if substring is a right brace and the top is a left brace
stack.pop(); //remove from top, continue searching
}
if (s.substring(i, i + 1).equals("{") || s.substring(i, i + 1).equals("}")){ //if substring is a parenthesis
if (s.substring(i, i + 1).equals("(")) // if is left parenthesis push onto stack
stack.push("(");
else if (!stack.peek().equals("(") || stack.size() <= 0) //otherwise, check if left parenthesis is not at top or if stack is empty
return 1;
else if (s.substring(i).equals(")") && stack.peek().equals("(")) //otherwise if substring is a right parenthesis and the top is a left parenthesis
stack.pop(); //remove from top, continue searching
}
if (stack.size() == 0) //
return 0;
else if (stack.size() != 0 && stack.peek().equals("("))
return 2;
else if (stack.size() != 0 && stack.peek().equals("{"))
return 4;
}
return 150; //checks for error
}
}
我的问题是程序输出所有内容都匹配(返回0),无论情况如何。这是我的主要方法:
import java.io.*;
public class Assignment11
{
public static void main (String[] args) throws IOException
{
char input1;
String inputInfo;
String line = new String();
printMenu();
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader stdin = new BufferedReader(isr);
do // will ask for user input
{
System.out.println("What action would you like to perform?");
line = stdin.readLine();
input1 = line.charAt(0);
input1 = Character.toUpperCase(input1);
if (line.length() == 1)
{
// matches one of the case statements
switch (input1)
{
case 'P': //Enter String
System.out.print("Please enter a string.\n");
inputInfo = stdin.readLine().trim();
int result = Matching.checking(inputInfo);
if (result == 0)
System.out.println("Everything is matching");
else if (result == 1)
System.out.println("right parenthesis does not have its matching left parenthesis");
else if (result == 2)
System.out.println("left parenthesis does not have its matching right parenthesis");
else if (result == 3)
System.out.println("right brace does not have its matching left brace");
else if (result == 4)
System.out.println("left brace does not have its matching right brace");
break;
case 'Q': //Quit
break;
case '?': //Display Menu
printMenu();
break;
default:
System.out.print("Unknown action\n");
break;
}
}
else
{
System.out.print("Unknown action\n");
}
} while (input1 != 'Q' || line.length() != 1);
}
/** The method printMenu displays the menu to a user**/
public static void printMenu()
{
System.out.print("Choice\t\tAction\n" +
"------\t\t------\n" +
"P\t\tEnter String\n" +
"Q\t\tQuit\n" +
"?\t\tDisplay Help\n\n");
}
}
非常感谢任何帮助或一般指示!
答案 0 :(得分:1)
查看您的第二个if
声明:
if (s.substring(i, i + 1).equals("{") || s.substring(i, i + 1).equals("}")){ //if substring is a parenthesis
评论表示它正在检查左右parens,但代码表示......