英文:
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<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!");
}
}
}
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 == '')' );
这一行上多加了一个不必要的分号,导致接下来的代码块总是会被执行。
英文:
You have a superfluous semicolon on the line else if ( c == ')' );
, causing the succeeding block to always be run.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论