处理错误:形成错误的字符常量(期待报价,得到h)

时间:2013-02-21 22:05:14

标签: java processing

我正在制作虚拟宠物游戏。在这个阶段,我已经加载了一个保存文件,但是当我开始包含核心功能时,程序没有运行。

我发现了这个:Badly formed character (expecting quote, got I) ~ Processing 这让我觉得这是我的加载功能的一个问题,但我是Processing的新手,我不知道如何调试它。

Creature creature;
String data[];
boolean gameInfo[];
int tempData[];
boolean food;

void setup() {
  size(100,100);
  String data[] = loadStrings("save.txt");
  String[] tempData = split(data[0], ',');
  gameInfo = int(tempData);
  for (int i = 0; i < data.length; i++) { 
    creature = new Creature(gameInfo[0], gameInfo[1], gameInfo[2], gameInfo[3]);
  }
  food = false;
}

void draw() {
  creature.think(food);
}

//creature class

class Creature {
  boolean idle;
  int hungry;
  int age;
  int gender;
  int asleep;


  Creature(int gender, int age, int hungry, int asleep) {
    this.gender = gender;
    this.age = age;
    this.hungry = hungry;
    this.asleep = asleep;
    boolean idle = true;
  }
  void whatDo(boolean food) {
    println('whatdo');
    if (idle = true && hungry == true) {
      if (food == true) {
        creature.eat();
      }
      else 
        creature.ask();
    }
  }

  void ask() {
    if (hungry == true) {
      println("HUNGRY");
    }
  }
  void eat() {
    println("EAT");
    idle = false;
  }
}

1 个答案:

答案 0 :(得分:2)

好吧,正如您指出的链接所解释的那样,问题是单引号用于字符赋值,因此它应该是两个单引号之间的字符,如'c',而println()需要定义一个字符串在双引号"a string"之间。只需用println('whatdo');(双引号)替换第40行中的println("whatdo");即可消除此错误。但是在修好之后我得到了其他错误。

相关问题