【问题标题】:Java: Why is my program outputting the else statement when it's not true? [closed]Java:为什么我的程序输出不正确的 else 语句? [关闭]
【发布时间】:2020-10-05 00:12:31
【问题描述】:

此程序读取用户输入,例如: (这个(是(我的))输入)

然后在遇到 '(' 时遍历将 '(' 添加到 Stack s 的语句 并在遇到 ')' 时从 Stack 中删除 '('。

然后它检查堆栈中是否还有任何东西,并根据特定类型的括号有多少不匹配来打印一条消息

// ********************************************************************
// ParenMatch.java
//
// Determines whether or not a string of characters contains
// matching left and right parentheses.
// ********************************************************************

import java.util.*;
import java.util.Scanner;

public class ParenMatch
{
    public static void main (String[] args)
    {
        Stack<Character> s = new Stack<Character>();
        String line; // the string of characters to be checked
        Scanner scan = new Scanner(System.in);
        System.out.println ("\nParenthesis Matching");
        System.out.print ("Enter a parenthesized expression: ");
        line = scan.nextLine();

        String goodSoFar = "";


        // Test: Here is a test (which should work (or maybe it doesn't) )

        // loop to process the line one character at a time
        for (int i = 0; i < line.length(); i++) 
        {
            char c = line.charAt(i);
            goodSoFar += c;      // add the character to the string so far

            if( c == '(' )
            {
                // open paren so add it to the stack
                s.push( line.charAt(i) );
            }
            else if ( c == ')' );
            {
                // hit close paren so pull open paren off the stack
                if( s.size() > 0 )
                    s.pop();
                
                else
                {
                    // stack does not have a matching paren so show an error!
                    System.out.println("Error! Close parenthesis without a matching open parenthesis!");
                    System.out.println("Error encountered here: " + goodSoFar + "^");
                }
            }
        }
        
        scan.close();

        // check final stack
        if( s.size() > 0 )
        {
            System.out.println("Error! There are " + s.size() + " extra open parenthesis!");
        }
        else
        {
            System.out.println("The number of open parenthesis matched the number of close parenthesis!");
        }

    }

}

我遇到的问题是当我的输入是:

(this (is (my))input)

那么我的输出是:

Enter a parenthesized expression: (this (is (my))input)
Error! Close parenthesis without a matching open parenthesis!
Error encountered here: (t^
Error! Close parenthesis without a matching open parenthesis!
Error encountered here: (th^
Error! Close parenthesis without a matching open parenthesis!
Error encountered here: (thi^
Error! Close parenthesis without a matching open parenthesis!
Error encountered here: (this^
Error! Close parenthesis without a matching open parenthesis!
Error encountered here: (this ^
Error! Close parenthesis without a matching open parenthesis!
Error encountered here: (this (i^
Error! Close parenthesis without a matching open parenthesis!
Error encountered here: (this (is^
Error! Close parenthesis without a matching open parenthesis!
Error encountered here: (this (is ^
Error! Close parenthesis without a matching open parenthesis!
Error encountered here: (this (is (m^
Error! Close parenthesis without a matching open parenthesis!
Error encountered here: (this (is (my^
Error! Close parenthesis without a matching open parenthesis!
Error encountered here: (this (is (my)^
Error! Close parenthesis without a matching open parenthesis!
Error encountered here: (this (is (my))^
Error! Close parenthesis without a matching open parenthesis!
Error encountered here: (this (is (my))i^
Error! Close parenthesis without a matching open parenthesis!
Error encountered here: (this (is (my))in^
Error! Close parenthesis without a matching open parenthesis!
Error encountered here: (this (is (my))inp^
Error! Close parenthesis without a matching open parenthesis!
Error encountered here: (this (is (my))inpu^
Error! Close parenthesis without a matching open parenthesis!
Error encountered here: (this (is (my))input^
Error! Close parenthesis without a matching open parenthesis!
Error encountered here: (this (is (my))input)^
The number of open parenthesis matched the number of close parenthesis!

但我对该输入的期望输出是:

The number of open parenthesis matched the number of close parenthesis!

【问题讨论】:

  • 您在else if ( c == ')' ) 之后有一个;。是不是打错字了?
  • 是的,这是导致我的问题的错字。谢谢!

标签: java for-loop if-statement


【解决方案1】:

else if ( c == ')' ); 行上有一个多余的分号,导致后面的块总是运行。

【讨论】:

    猜你喜欢
    • 2016-08-10
    • 1970-01-01
    • 2019-03-24
    • 2019-04-25
    • 2014-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-22
    相关资源
    最近更新 更多