Java 如何根据用户输入的数字移动机器人?

Java 如何根据用户输入的数字移动机器人?,java,Java,我使用drjava-stable-20120818-r5686进行编码 class Assignment2 { public static void main(String[] args) { World barney=new World(20,20); Robot bob=new Robot(); barney.addBeeper(6,4); barney.add(bob,4,4,"north");

我使用drjava-stable-20120818-r5686进行编码

    class Assignment2
    {
     public static void main(String[] args)
         {
      World barney=new World(20,20);
      Robot bob=new Robot();
      barney.addBeeper(6,4);
      barney.add(bob,4,4,"north");
      System.out.println("1. Turn left from current direction.");
     System.out.println("2. Turn right from current direction.");
     System.out.print("Enter Choice: ");
     int choice=Console.readInt();

     if(choice==1)
     {
     bob.turnLeft();
     }
     else
     {
     bob.turnLeft();
     bob.turnLeft();
     bob.turnLeft();
     }

     System.out.println("1. Move Forward Once.");
     System.out.println("2. Move Forward Twice.");
     System.out.println("3. Move Forward X Times.");
     System.out.println("Enter Choice: ");
     int choice2=Console.readInt();
     if(choice2==1)
     {
       bob.move();
     }
     if(choice2==2)
     {
        bob.move();
         bob.move();
     }
     else
     {
       System.out.println("Enter X: "); //dont know what to do next :(
    }
    [enter image description here][1]`
**我们的老师说用loop(while)来做,但我不确定怎么做 **

类似于:

System.out.println("Enter X: ");
int x =Console.readInt();
while(x>0) {
         bob.move();
         x--;
         }

请查看Oracle文档中关于语法的部分,如下所述:

while(表达式){ 声明(s) }

使用这个,下面的代码应该可以解决您的问题

int X=Console.readInt();
while(X-->0){ bob.move();}

也许问问你的老师?在这里得到答案对你没有帮助。向你的老师请教或者按照一些教程来理解语言的基础知识。然后你就可以解决这个问题。或者你的同学们(你可能已经知道了,这只是以防万一)。对于这类问题,我建议你解释一下这段代码。一般来说,这对于一个简单的代码来说是不需要的,但是你正在回答一个新的开发人员,所以他可能不理解或者只是复制了这个代码。谢谢@Khalil你让我看起来很愚蠢:))谢谢你的建议。下次我会考虑的。我建议你阅读,尤其是关于答案的链接。我读了它,这是我能得到解释的距离。我还缺少什么吗?这样更好:)你可以简单地解释什么是表达式,什么是语句,但你很快就会写一篇关于Java的教程;)