是的,我知道这个错误比普通流感更常见,不,我不知道如何解决它。因为坦率地说,我不知道它在这个特定实例上意味着什么。我有非常简单的代码来检查括号,但由于这个原因它没有编译,我不知道为什么。
错误在测试类中,第 14 行。
但这是我唯一的两门课。
import java.util.Stack;
import java.util.EmptyStackException;
class Arithmetic
{
Stack <Object>stk;
String expression;
int length;
Arithmetic(String s)
{
expression = s;
length = expression.length();
stk = new Stack<Object>();
}
boolean isBalanced()
{
int index = 0;
boolean fail = false;
try
{
while(index < length)
{
char ch = expression.charAt(index);
switch(ch)
{
case '(':
stk.push(ch);
break;
case ')':
stk.pop();
break;
default:
//ignore all others chars.
break;
}
index++; //increment index to continue on to the next char.
}
}
catch(EmptyStackException e)
{
fail = true;
}
return fail;
}
}
有错误的那个:
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
class TestFile
{
public static void main(String[] arg)
{
String menu = "Please enter an arithmetic expression to evaluate for balance:";
String s = JOptionPane.showInputDialog( menu);
display(s);
new Arithmetic(s);
boolean balanced = Arithmetic.isBalanced();
display(balanced);
}
static void display(boolean Boolean)
{
JOptionPane.showMessageDialog(null, Boolean, "Content", JOptionPane.INFORMATION_MESSAGE);
}
static void display(String s)
{
JOptionPane.showMessageDialog(null, s, "Content", JOptionPane.INFORMATION_MESSAGE);
}
}