标记“;"、“{"的语法错误预期在此令牌之后

编程入门 行业动态 更新时间:2024-10-26 12:28:51
本文介绍了标记“;"、“{"的语法错误预期在此令牌之后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

只是一个简单的类调用一个打印数组的类.我在 Eclipse 中遇到语法错误.我还收到一个错误,说我没有名为 Kremalation 的方法.

Just a simple class calls a class that prints an array. I get a syntax error in Eclipse. I also get an error that I don't have a method called Kremalation.

public class AytiMain {

    public static void main(String[] args) {
        AytiMain.Kremalation();
    }
}

public class Kremalation {

    String[] ena = { "PEINAW", "PEINOUSA", "PETHAINW" };
    int i; // <= syntax error on token ";", { expected after this token

    for (i = 0; i <= ena.lenght; i++)
        System.out.println(ena[i]);
}
}

推荐答案

您在方法之外有代码(不是声明变量和/或初始化它),即:

You have code (which is not declaring a variable and/or initializing it) ouside a method, which is:

for (i=0; i<=ena.lenght; i++)
    System.out.println(ena[i]);

在 Java 中,代码必须驻留在方法中.你不能调用一个类,你必须调用一个在内部声明的方法.

In Java, code MUST reside inside a method. You can't call a class, you have to call a method that is declared inside a class.

错误:

class ClassName {
   for (...) 
}

正确:

class ClassName {
  static void method() {
    for (...)
  }

  public static void main(String[] args) {
    ClassName.method();
  }
}

这篇关于标记“;"、“{"的语法错误预期在此令牌之后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 13:03:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1410204.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:在此   令牌   标记   语法错误   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!