编译Java类,“类型非法开始”和“预期<identifier>”错误

时间:2019-03-01 14:49:42

标签: java java-11

在练习使用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!");
    }
}

主要方法是要从控制台中获取一些输入,并使用来自地图的数据(“敌人”)或默认输入(取决于输入)来创建特定的输出。但是,当我尝试在命令提示符下进行编译时,我在所有敌人..()方法上都遇到了错误,即:

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

2 个答案:

答案 0 :(得分:0)

菱形符号可以告诉编译器有关泛型类的信息。但是,语法应为:

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

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

答案 1 :(得分:0)

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