0

好的,所以我正在尝试创建一个程序,使用扫描仪,如果用户键入“上”、“下”、“左”或“右”,形状将相应移动。我让形状按照单词所说的那样移动,但是每当我输入新命令时,我都无法让程序记住在给出新命令之前形状在哪里。每次我进入新方向时,形状都会从 0,0 开始。这是我到目前为止所拥有的,我相信问题出在我的 y 坐标上:

 double location = 0;
 double prev = 0;     
 for (double i = -10; i <= 10; i++) // for the scale that was set 
 {
  String word = in.nextLine(); // new string: word = user typed

  if (word.equals("up")) // if word = up, move player up
  {
    location ++;
    prev -= location;
    StdDraw.clear();
    drawTarget(a);
    StdDraw.setPenColor(StdDraw.BLUE);
    StdDraw.filledCircle(prev, location, 0.5);

  }

 else if (word.equals("down")) // if word = down, move player down
  {
    location--;
    prev += location;
    StdDraw.clear();
    drawTarget(a);
    StdDraw.setPenColor(StdDraw.BLUE);
    StdDraw.filledCircle(prev, location, 0.5);

  }
 }
4

0 回答 0