【问题标题】:Compiling java class, 'illegal start of type' and '<identifier> expected' errors编译 java 类,“类型的非法开始”和“<identifier> 预期”错误
【发布时间】:2019-03-01 14:49:41
【问题描述】:

正在练习使用 Java 中的构造函数,这是我试图编译的类:

import java.util.*;
import java.lang.*;
public class mob{

    public Map enemies = new HashMap<String, Point>();
    public Point pn = new Point(1, 1);
    enemies.put("Peon", pn);
    public Point gn = new Point(5, 2);
    enemies.put("Goblin", gn);
    public Point tl = new Point(25, 8);
    enemies.put("Troll", tl);
    public Point oc = new Point(13, 5);
    enemies.put("Orc", oc);
    public String name;
    public int hp;
    public int dmg;

    public mob(String type){
      name=type;
      hp=enemies.get(type).getX();
      dmg=enemies.get(type).getY();
    }

    public mob(){
      name="Peon";
      hp=enemies.get("Peon").getX();
      dmg=enemies.get("Peon").getY();

    }

    void setName(String name){
            this.name=name;
    }


    public static void main(String[] args) {

      Scanner scan = new Scanner(System.in());
      System.out.System.out.println("Enter mob type:");
      String type = scan.nextln();
      if(mob.containsKey(type)){
        mob mob1 = new mob(type);
      }
      else{
        mob mob1 = new mob();
        mob1.setName(type);
      }

      System.out.println("You just spawned a "+mob1.name+", it has "+mob1.hp+" hp and "+mob1.dmg+" dmg!");
    }
}

主要方法旨在从控制台获取一些输入,并根据输入使用地图('敌人')或默认暴民的数据创建特定暴民。但是当我尝试在命令提示符中编译它时,我在所有的敌人.put() 方法中都遇到了错误,即:

mob.java:7: error: <identifier> expected
    enemies.put("Peon", pn);
               ^
mob.java:7: error: illegal start of type
    enemies.put("Peon", pn);
               ^
mob.java:9: error: <identifier> expected
    enemies.put("Goblin", gn);
               ^

等等

查看了类似的问题并尝试解决问题(因此所有公开声明)但无法解决这个问题......

使用Java和Javac都是v11.0.2

【问题讨论】:

  • enemies.put("Peon", pn); 等代码必须在方法、构造函数或初始化块中。它不能只在类体中。
  • 你能用一个普通的 IDE 来立即指出问题所在吗?
  • @greg-449 ty 还有一些其他问题,但这绝对有帮助!
  • @yegodm 尝试按照您的建议在 ide 中运行它,同样的错误。

标签: java java-11


【解决方案1】:

菱形用于告诉编译器有关泛型类的信息。但是,语法应该是:

 public Map<String,Point> enemies = new HashMap<>();

并且默认的the enemies.put("Peon", pn); 命令必须在构造函数中进行。

【讨论】:

  • 涵盖了我需要修复的所有内容,tyvm!
【解决方案2】:

Java 中的语句必须在方法中。看起来您正在尝试使用 enemies.put("Goblin", gn); 之类的语句初始化字段,因此我建议您为每个字段创建一个初始化方法。你的代码会变成这样:

import java.util.*;
import java.lang.*;
public class mob{

    public Map enemies = initialiseEnemies();
    public Point pn = new Point(1, 1);
    public Point gn = new Point(5, 2);
    public Point tl = new Point(25, 8);
    public Point oc = new Point(13, 5);
    public String name;
    public int hp;
    public int dmg;

    public static Map initialiseEnemies() {
        // Build the Hashmap here and return it.
    }

    public mob(String type){
      name=type;
      hp=enemies.get(type).getX();
      dmg=enemies.get(type).getY();
    }

    public mob(){
      name="Peon";
      hp=enemies.get("Peon").getX();
      dmg=enemies.get("Peon").getY();

    }

    void setName(String name){
            this.name=name;
    }


    public static void main(String[] args) {

      Scanner scan = new Scanner(System.in());
      System.out.System.out.println("Enter mob type:");
      String type = scan.nextln();
      if(mob.containsKey(type)){
        mob mob1 = new mob(type);
      }
      else{
        mob mob1 = new mob();
        mob1.setName(type);
      }

      System.out.println("You just spawned a "+mob1.name+", it has "+mob1.hp+" hp and "+mob1.dmg+" dmg!");
    }
}

【讨论】:

    猜你喜欢
    • 2021-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-17
    • 2019-02-06
    相关资源
    最近更新 更多