work.java

来自「模拟机器人在平原上的行进行为」· Java 代码 · 共 55 行

JAVA
55
字号
package robot;

public class Work {
	private String con;
	private int q1;
    private int s1;
    private char direction;
	public Work(String num)
	{
		con=num;
	}
	public void move(Robot r)/*行进方法*/
	{   q1=r.getq1();
        s1=r.gets1();
	    char direction=r.getdirection();
		switch(direction)
	   {case 'N': q1=q1+1; break;
		case 'S': q1=q1-1; break;
		case 'E': s1=s1+1; break;
		case 'W': s1=s1-1; break;
	   }	
		r.setq1(q1);
		r.sets1(s1);
	}
    
	public void turn(char control,Robot r)/*转向方案*/
	{
		direction=r.getdirection();
	 switch(direction)
	 {case 'N': if(control=='L') direction='W';
	            else direction='E'; break;
	  case 'S': if(control=='L') direction='E';
	            else direction='W'; break;
	  case 'E': if(control=='L') direction='N';
	            else direction='S'; break;
	  case 'W': if(control=='L') direction='S';
	            else direction='N'; break;          	 
	 }
	 r.setdirection(direction);
	}
	public void moving(String num,Robot r)/*移动方案*/
	{int length=num.length();
	 for(int z=0;length!=0;z++,length--)
	 {switch(num.charAt(z))
		 {case 'M': move(r); break;
		  case 'L': turn('L',r);break;
		  case 'R': turn('R',r);break;
		 }
	 }
	  
	}
	

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?