Java:为什么我的程序在条件不为真时输出了 else 语句?

huangapple go评论83阅读模式
英文:

Java: Why is my program outputting the else statement when it's not true?

问题

这个程序读取用户的输入,例如:
(this (is (my))input)
然后迭代地遍历该语句,在遇到'('时将'('添加到堆栈s中,在遇到')'时将'('从堆栈s中移除。

然后,它检查堆栈中是否还有剩余内容,并根据特定类型的括号不匹配情况打印相应的消息。

// ********************************************************************
// ParenMatch.java
//
// 判断一个字符序列中的左右括号是否匹配。
// ********************************************************************

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

public class ParenMatch
{
    public static void main (String[] args)
    {
        Stack<Character> s = new Stack<Character>();
        String line; // 要检查的字符序列
        Scanner scan = new Scanner(System.in);
        System.out.println ("\n括号匹配");
        System.out.print ("输入一个带括号的表达式: ");
        line = scan.nextLine();

        String goodSoFar = "";


        // 测试:这里是一个测试(应该可以工作(或者可能不能))

        // 循环逐个处理字符
        for (int i = 0; i < line.length(); i++) 
        {
            char c = line.charAt(i);
            goodSoFar += c;      // 将字符添加到目前为止的字符串中

            if( c == '(' )
            {
                // 遇到左括号,将其加入堆栈
                s.push( line.charAt(i) );
            }
            else if ( c == ')' )
            {
                // 遇到右括号,从堆栈中弹出左括号
                if( s.size() > 0 )
                    s.pop();
                
                else
                {
                    // 堆栈中没有匹配的左括号,显示错误!
                    System.out.println("错误!缺少匹配的左括号!");
                    System.out.println("错误位置: " + goodSoFar + "^");
                }
            }
        }
        
        scan.close();

        // 检查最终堆栈
        if( s.size() > 0 )
        {
            System.out.println("错误!有 " + s.size() + " 个多余的左括号!");
        }
        else
        {
            System.out.println("左括号的数量与右括号的数量匹配!");
        }

    }

}

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

(this (is (my))input)

那么输出为:

输入一个带括号的表达式: (this (is (my))input)
错误!缺少匹配的左括号!
错误位置: (t^
错误!缺少匹配的左括号!
错误位置: (th^
错误!缺少匹配的左括号!
错误位置: (thi^
错误!缺少匹配的左括号!
错误位置: (this^
错误!缺少匹配的左括号!
错误位置: (this ^
错误!缺少匹配的左括号!
错误位置: (this (i^
错误!缺少匹配的左括号!
错误位置: (this (is^
错误!缺少匹配的左括号!
错误位置: (this (is ^
错误!缺少匹配的左括号!
错误位置: (this (is (m^
错误!缺少匹配的左括号!
错误位置: (this (is (my^
错误!缺少匹配的左括号!
错误位置: (this (is (my)^
错误!缺少匹配的左括号!
错误位置: (this (is (my))^
错误!缺少匹配的左括号!
错误位置: (this (is (my))i^
错误!缺少匹配的左括号!
错误位置: (this (is (my))in^
错误!缺少匹配的左括号!
错误位置: (this (is (my))inp^
错误!缺少匹配的左括号!
错误位置: (this (is (my))inpu^
错误!缺少匹配的左括号!
错误位置: (this (is (my))input^
错误!缺少匹配的左括号!
错误位置: (this (is (my))input)^
左括号的数量与右括号的数量匹配!

但我期望的输出是:

左括号的数量与右括号的数量匹配!
英文:

This program reads user input for example:
(this (is (my))input)

and then iterate over the statement adding '(' to Stack s when '(' is encountered
and removing '(' from Stack s when ')' is encountered.

Then it checks if there is anything left in the stack and prints a message depending on how many of a particular type of parenthesis are mismatch

// ********************************************************************
// 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&lt;Character&gt; s = new Stack&lt;Character&gt;();
String line; // the string of characters to be checked
Scanner scan = new Scanner(System.in);
System.out.println (&quot;\nParenthesis Matching&quot;);
System.out.print (&quot;Enter a parenthesized expression: &quot;);
line = scan.nextLine();
String goodSoFar = &quot;&quot;;
// Test: Here is a test (which should work (or maybe it doesn&#39;t) )
// loop to process the line one character at a time
for (int i = 0; i &lt; line.length(); i++) 
{
char c = line.charAt(i);
goodSoFar += c;      // add the character to the string so far
if( c == &#39;(&#39; )
{
// open paren so add it to the stack
s.push( line.charAt(i) );
}
else if ( c == &#39;)&#39; );
{
// hit close paren so pull open paren off the stack
if( s.size() &gt; 0 )
s.pop();
else
{
// stack does not have a matching paren so show an error!
System.out.println(&quot;Error! Close parenthesis without a matching open parenthesis!&quot;);
System.out.println(&quot;Error encountered here: &quot; + goodSoFar + &quot;^&quot;);
}
}
}
scan.close();
// check final stack
if( s.size() &gt; 0 )
{
System.out.println(&quot;Error! There are &quot; + s.size() + &quot; extra open parenthesis!&quot;);
}
else
{
System.out.println(&quot;The number of open parenthesis matched the number of close parenthesis!&quot;);
}
}
}

The problem I'm having is that when my input is:

(this (is (my))input)

Then my output is:

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!

But my desired output for that input is:

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

答案1

得分: 4

你在 else if ( c == '&#39;)' ); 这一行上多加了一个不必要的分号,导致接下来的代码块总是会被执行。

英文:

You have a superfluous semicolon on the line else if ( c == &#39;)&#39; );, causing the succeeding block to always be run.

huangapple
  • 本文由 发表于 2020年10月5日 08:12:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64201053.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定