如何跳过爪哇的下一个角色

发布于 2025-02-13 05:06:36 字数 1894 浏览 1 评论 0原文

我正在研究将前缀转换为后缀表达式的程序。但是,当表达式中有一个意外的空白空间,例如“ $+ - ABC+de f”,而不是“ $+ - abc+d-ef”,程序无法正常工作。我如何写下下一个字符并忽略空格,试图通过使用布尔iSblank方法的else语句进行操作。

public class PrefixConverter{

    // Checks if character is an operator
    public boolean isOperator(char c){
        switch (c){
            case '+':
            case '-':
            case'*':
            case'/':
            case'$':
                return true;
        }
        return false;
    }

    // Ignores white space
    public boolean isBlank(char c){
        switch (c){
        case ' ':
        return true;
        }
        return false;
    }

    // Method to convert Prefix expression to Postfix expression
    public String preToPost (String prefix_exp){

        // Create a new stack with length of the prefix string
        int size = prefix_exp.length();
        Stack expression_stack = new Stack (size);

        // Read expression from right to left
        for (int i = size -1; i >=0 ; i-- ){

            if (isOperator(prefix_exp.charAt(i))){

                // Pop two operands from the stack
                String op1 = expression_stack.peek();
                expression_stack.pop();
                String op2 = expression_stack.peek();
                expression_stack.pop();

                // Concatenate the operands and the operator
                String temp = op1 + op2 + prefix_exp.charAt(i);

                // Push the result back onto the stack
                expression_stack.push(temp);
            }

            else if(isBlank(prefix_exp.charAt(i))){
                // Skip to next character
            }

            // If the symbol is an operand
            else {
                // Push the operand onto the stack
                expression_stack.push(prefix_exp.charAt(i) + "");
            }
        }

        return expression_stack.peek();
    }
}

I am working on a program that converts a prefix to a postfix expression. However, when there is an unexpected blank space in an expression such as "$+-ABC+D-E F" instead of "$+-ABC+D-EF" the program doesn't work correctly. How do I write skip to the next character and ignore the whitespace, trying to do it through an if else statement using a boolean isBlank method.

public class PrefixConverter{

    // Checks if character is an operator
    public boolean isOperator(char c){
        switch (c){
            case '+':
            case '-':
            case'*':
            case'/':
            case'
:
                return true;
        }
        return false;
    }

    // Ignores white space
    public boolean isBlank(char c){
        switch (c){
        case ' ':
        return true;
        }
        return false;
    }

    // Method to convert Prefix expression to Postfix expression
    public String preToPost (String prefix_exp){

        // Create a new stack with length of the prefix string
        int size = prefix_exp.length();
        Stack expression_stack = new Stack (size);

        // Read expression from right to left
        for (int i = size -1; i >=0 ; i-- ){

            if (isOperator(prefix_exp.charAt(i))){

                // Pop two operands from the stack
                String op1 = expression_stack.peek();
                expression_stack.pop();
                String op2 = expression_stack.peek();
                expression_stack.pop();

                // Concatenate the operands and the operator
                String temp = op1 + op2 + prefix_exp.charAt(i);

                // Push the result back onto the stack
                expression_stack.push(temp);
            }

            else if(isBlank(prefix_exp.charAt(i))){
                // Skip to next character
            }

            // If the symbol is an operand
            else {
                // Push the operand onto the stack
                expression_stack.push(prefix_exp.charAt(i) + "");
            }
        }

        return expression_stack.peek();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

留一抹残留的笑 2025-02-20 05:06:37

一种方法是在else中写下继续;如果():

        else if(isBlank(prefix_exp.charAt(i))){
            // Skip to next character
            continue;
        }

继续将简单地移至循环的下一个迭代,

但是,如果您不需要空格您可以在开始时从prefix_exp字符串中删除它们:

prefix_exp = prefix_exp.replaceAll("\\s", "");

只需在尺寸变化时调用字符串的.length()之前进行替换。

One way would be to write a continue; in this else if():

        else if(isBlank(prefix_exp.charAt(i))){
            // Skip to next character
            continue;
        }

continue will simply move to the next iteration of the loop

However, if you do not need the spaces you can remove them from the prefix_exp String in the beginning by doing this:

prefix_exp = prefix_exp.replaceAll("\\s", "");

Just make sure you do the replaceAll before you call .length() of the String as the size changes.

逆夏时光 2025-02-20 05:06:37

只需使用继续语句即可跳到循环的末尾。
这将触发循环以使用下一个字符运行。但是,由于您的其余代码都在IF语句中,因此您的代码不做任何事情都应该表现良好。

...
else if(isBlank(prefix_exp.charAt(i))){
    // Skip to next character
    continue;
}

另请参见 https://docs.oracle.com/javase.com/javase/tutorial/ java/nutsandbolts/branch.html

Just use the continue statement to skip to the end of your for loop.
This will trigger the loop to run with the next character. But since the rest of your code is in if statements anyway, your code should behave well by just doing nothing.

...
else if(isBlank(prefix_exp.charAt(i))){
    // Skip to next character
    continue;
}

See also https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文