错误:非法启动类型 [英] error: illegal start of type

查看:481
本文介绍了错误:非法启动类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这小块代码在第6行和第10行(for循环)中给出非法开始的类型错误....我找不到任何不匹配的大括号...

why this little piece of code is giving illegal start of type error in line 6 and 10(for loops).... i can't find any unmatched braces...

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());
    }
}



我有Stack实现类,

I have the class Stack implemented,

推荐答案

你不能在类体中使用for循环,你需要把它们放在某种方法中。

You cannot use for loop inside a class body, you need to put them in some kind of method.

class StackDemo{
final int size = 10;
Stack s = new Stack(size);
public void run(){
   //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());
   }
   }
}

这篇关于错误:非法启动类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆