编译 java class、'illegal start of type' 和“<identifier> 预期的”错误

Compiling java class, 'illegal start of type' and '<identifier> expected' errors

在 Java 中练习使用构造函数,这里是我尝试编译的 class:

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!");
    }
}

主要方法旨在从控制台获取一些输入,并根据输入使用地图 ('enemies') 或默认生物中的数据创建特定生物。但是当我尝试在命令提示符中编译它时,我在所有 enemies.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);
               ^

等等

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

使用Java和Javac是v11.0.2

钻石在那里是为了告诉编译器有关泛型的信息 类。但是,语法应该是:

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

和默认 the enemies.put("Peon", pn); 命令必须在 构造函数 .

中执行

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!");
    }
}