Sou*_*aji -1 java stack compiler-errors
为什么这一小段代码在第6行和第10行(for循环)中给出了非法的类型错误启动....我找不到任何不匹配的大括号......
class StackDemo{
final int size = 10;
Stack s = new Stack(size);
//Push charecters into the stack
for(int i=0; i<size; i++){
s.push((char)'A'+i);
}
//pop the stack untill its empty
for(int i=0; i<size; i++){
System.out.println("Pooped element "+i+" is "+ s.pop());
}
}
Run Code Online (Sandbox Code Playgroud)
我实现了Stack类,
你不能for在类级别使用循环.把它们放在一个method或一个block
同样java.util.Stack在Java不具备这样的构造.
它应该是
Stack s = new Stack()
Run Code Online (Sandbox Code Playgroud)
另一个问题
s.push(char('A'+i))// you will get Unexpected Token error here
Run Code Online (Sandbox Code Playgroud)
只需将其更改为
s.push('A'+i);
Run Code Online (Sandbox Code Playgroud)