Java 字符堆栈来检查匹配的括号没有错误,但也没有任何作用

Java 字符堆栈来检查匹配的括号没有错误,但也没有任何作用,java,stack,Java,Stack,我花了半年的时间停止编程,现在我只是用一些超级基本的面试小问题来刷新我的记忆……第一个问题我已经被困了两天。。下面是我的代码,有人能告诉我为什么程序没有显示错误,但编译后什么也不做吗?它应该忽略文本字符,但我甚至还没有 public class bracketCheck { public static void main(String[] args) { Stack <Character> s = new <Character> Stack(); Scanner

我花了半年的时间停止编程,现在我只是用一些超级基本的面试小问题来刷新我的记忆……第一个问题我已经被困了两天。。下面是我的代码,有人能告诉我为什么程序没有显示错误,但编译后什么也不做吗?它应该忽略文本字符,但我甚至还没有

public class bracketCheck {

public static void main(String[] args) {


Stack <Character> s = new <Character> Stack(); 
Scanner input = new Scanner(System.in);
String buff; 

System.out.println("please enter brackets & text");
buff = input.nextLine(); 
input.close();
int count = 0;
boolean cont = true;
Character stackTop;
Character current = buff.charAt(count);
do {
    if(current == ')' || current== '}' || current== ']') {
        s.push(current);
        count++;
    }
    else if(current== '(' || current== '{' || current== '[') {
        stackTop = s.pop();
        cont = match(stackTop, current);    
    }
}
while(s.isEmpty() == false /*&& cont =true*/); 
if(s.isEmpty())
    System.out.println("bout time......");

}

private static boolean match(Character top , Character not) {
    if(top == ')' && not == '(') 
        return true;
    else if(top == ']' && not == '[') 
        return true;
    else if(top == '}' && not == '{') 
        return true;
    else 
        return false;
}

}
公共类括号检查{
公共静态void main(字符串[]args){
堆栈s=新堆栈();
扫描仪输入=新扫描仪(System.in);
琴弦浅黄色;
System.out.println(“请输入括号和文本”);
buff=input.nextLine();
input.close();
整数计数=0;
布尔控制=真;
字符栈顶;
字符电流=buff.charAt(计数);
做{
如果(当前==')'| |当前=='}'| |当前==']'){
s、 推动(电流);
计数++;
}
else if(当前=='('| |当前=='{'| |当前=='['){
stackTop=s.pop();
cont=匹配(堆栈顶部,当前);
}
}
而(s.isEmpty()==false/*&&cont=true*/);
如果(s.isEmpty())
System.out.println(“关于时间……”);
}
私有静态布尔匹配(字符顶部,字符非顶部){
if(top==')和¬=='(')
返回true;
else if(top=']'&¬='[')
返回true;
else if(top='}'&¬='{')
返回true;
其他的
返回false;
}
}

我在您的代码中看到了一些问题,这是我的固定版本

import java.util.Scanner;
import java.util.Stack;

// use Capital letters in the beginning of class names
public class BracketCheck {
    public static void main(String[] args) {

        Stack<Character> stack = new Stack<>();
        Scanner input = new Scanner(System.in);
        String buff;

        System.out.println("please enter brackets & text");
        buff = input.nextLine();
        input.close();
        // using java8 makes iterating over the characters of a string easier
        buff.chars().forEach(current -> {
            // if <current> is an opening bracket, push it to stack
            if (current == '(' || current == '{' || current == '[') {
                stack.push((char) current);
            }
            // if <current> is a closing bracket, make sure it is matching an opening
            // bracket or alert and return
            else if (current == ')' || current == '}' || current == ']') {
                if (!match(stack, (char) current)) {
                    System.out.println("no good");
                    return;
                }
            }
        });
        // if, after we finished iterating the string, stack is empty, all opening
        // brackets had matching closing brackets
        if (stack.isEmpty()) {
            System.out.println("bout time......");
        }
        // otherwise, alert
        else {
            System.out.println("woah");
        }
    }

    private static boolean match(Stack<Character> stack, Character closer) {
        // if stack is empty, the closer has no matching opener
        if (stack.isEmpty()) {
            return false;
        } else {
            // get the most recent opener and verify it matches the closer
            Character opener = stack.pop();
            if (opener == '(' && closer == ')')
                return true;
            else if (opener == '[' && closer == ']')
                return true;
            else if (opener == '{' && closer == '}')
                return true;
            else
                return false;
        }
    }
}
import java.util.Scanner;
导入java.util.Stack;
//在类名的开头使用大写字母
公共类括号检查{
公共静态void main(字符串[]args){
堆栈=新堆栈();
扫描仪输入=新扫描仪(System.in);
琴弦浅黄色;
System.out.println(“请输入括号和文本”);
buff=input.nextLine();
input.close();
//使用java8可以更容易地迭代字符串的字符
buff.chars().forEach(当前->{
//如果是开口支架,则将其推至堆叠
如果(当前==”(“| |当前==”{“| |当前==”[”){
stack.push((字符)当前值);
}
//如果是闭合支架,请确保它与开口相匹配
//括号或警告和返回
如果(当前==')'| |当前=='}'| |当前==']'),则为else{
如果(!匹配(堆栈,(字符)当前)){
System.out.println(“不好”);
返回;
}
}
});
//如果在完成字符串迭代后,堆栈为空,则所有堆栈都打开
//括号中有匹配的结束括号
if(stack.isEmpty()){
System.out.println(“关于时间……”);
}
//否则,请保持警惕
否则{
System.out.println(“woah”);
}
}
私有静态布尔匹配(堆栈、字符闭合器){
//如果堆栈为空,则关闭器没有匹配的打开器
if(stack.isEmpty()){
返回false;
}否则{
//获取最新的开场白,并验证它是否与最新的开场白匹配
字符开启器=stack.pop();
如果(开场白=='('&&closer==')')
返回true;
else if(开场白=='['&&closer==']')
返回true;
else if(opener=='{'&&closer=='}')
返回true;
其他的
返回false;
}
}
}

您得到的输出是什么?作为
buff
传递的是什么?嘿,在回答您的具体问题之前,请注意cont没有被读取(您已经注释掉了),Stack s=new Stack();是初始化s(这不是变量的好名称)@gblodget java.util.Scanner[delimiters=\p{javaWhitespace}+][position=7][match valid=true][need input=false][source closed=true][skipped=false][group separator=\,][decimal separator=\.][正前缀=][负前缀=\Q-\E][正后缀=][负后缀=][NaN string=\Q�\E] [无限字符串=\Q∞\E] @Royshahaaf我的想法是一次只发一条短信。首先,至少要检查大括号是否被推和弹出,因为我不知道问题出在哪里。我的想法是一次移动一步。至于变量名,他说