java左转两位_Java面试编程题-火星车、火星漫游车

题目描述如下:

一队机器人漫游车将被美国宇航局降落在火星高原上。漫游车将在这个奇怪的长方形高原上巡游,以便他们的机载摄像头可以获得周围地形的完整视图,并将其发送回地球。漫游者的坐标和位置由x和y坐标的组合以及代表四个方向(E, S, W, N)的字母表示。高原划分为网格以简化导航。比如位置0,0,N,表示漫游车位于左下角并面向北。为了控制漫游车,美国宇航局发送一串简单的字母。指令字母是'L','R'和'M'。 'L'和'R'使漫游车分别向左或向右旋转90度,而不会从当前地点移动。 'M'表示前进一个网格点,并保持相同的方向。

假设从(x,y)直接向北移动,就到了(x,y + 1)。

INPUT:

第一行输入是平台的右上角坐标,左下角坐标被假定为0,0。

其余的输入是有关已部署的漫游车的信息。每个漫游车都有两行输入。第一行给出了漫游车的位置,第二行是告诉漫游车如何探索高原的一系列指令。位置由两个整数和一个由空格分隔的字母组成,对应于x和y坐标以及漫游车当前的方向。

每个漫游车将按顺序完成,这意味着第二个漫游车在第一个漫游车完成移动之前不会开始移动。

OUTPUT:

每个漫游车的输出应该是其最终的坐标和位置。

输入输出例子

输入:

5 5

1 2 N

LMLMLMLMM

3 3 E

MMRMMRMRRM

预期产出:

1 3 N

5 1 E

代码如下:

public class MarsCarTest {

public static void main(String[] args) throws Exception{

Scanner sc = new Scanner(System.in);

List carList = new ArrayList();

System.out.println("请输入平台右上角坐标");

MarsCar.Max_x = sc.nextInt();

MarsCar.Max_y = sc.nextInt();

System.out.println("命令输入完毕后请输入 EXIT 退出");

while(!sc.hasNext("EXIT")){

int x = sc.nextInt();

int y = sc.nextInt();

String fx = sc.next();

getCoordinate(x, y, fx, carList, sc);

}

if(carList!=null && carList.size()>0){

MarsCar car = null;

for(int i=0; i

car = carList.get(i);

System.out.println(car.getX()+" "+car.getY()+" "+car.getFx());

}

} else {

System.out.println("暂无车辆坐标信息!");

}

sc.close();

carList.clear();

}

//得到每辆车的最终坐标存入集合中,命令结束后打印输出

public static void getCoordinate(int x, int y, String fx, List carList, Scanner sc){

MarsCar car = new MarsCar(x,y,fx);

if(car.inspect()){

String str = sc.next();

if(str!=null && str.length()>0){

boolean isSuccess = true;//标记若是命令错误则提示重输递归

for(int i=0; i

if("M".equals(String.valueOf(str.charAt(i)))){

car.move();

} else if("L".equals(String.valueOf(str.charAt(i)))){

car.gotoLeft();

} else if("R".equals(String.valueOf(str.charAt(i)))){

car.gotoRight();

} else {

System.out.println("命令输入错误,请重新输入!");

isSuccess = false;

break;

}

}

if(isSuccess){

carList.add(car);

} else {

getCoordinate(x, y, fx, carList, sc);

}

}

} else {

System.out.println("坐标输入错误,请重新输入!");

}

}

}

---------class  MarsCar-----------------分割线--------------------

class MarsCar {

private int x;//x坐标值

private int y;//y坐标值

private String fx;//方向

static int Max_x;//最大x值

static int Max_y;//最大y值

public MarsCar(){

}

public MarsCar(int x, int y, String fx){

this.x = x;

this.y = y;

this.fx = fx.trim();

}

//检查坐标是否合格

public boolean inspect(){

if(this.x<0||this.x>Max_x||this.y<0||this.y>Max_y||!("E".equals(this.fx)||"W".equals(this.fx)||"S".equals(this.fx)||"N".equals(this.fx))){

return false;

}

return true;

}

public int getX() {

return x;

}

//x坐标限制

public void setX(int x) {

if(x<=Max_x && x>=0){

this.x = x;

}

}

public int getY() {

return y;

}

//y坐标限制

public void setY(int y) {

if(y<=Max_y && y>=0){

this.y = y;

}

}

public String getFx() {

return fx;

}

public void setFx(String fx) {

if("E".equals(fx)||"S".equals(fx)||"W".equals(fx)||"N".equals(fx)){

this.fx = fx.trim();

}

}

//前进一步

public void move(){

if("E".equals(this.fx)){

this.x += 1;

} else if("S".equals(this.fx)){

this.y -= 1;

} else if("W".equals(this.fx)){

this.x -= 1;

} else if("N".equals(this.fx)){

this.y += 1;

}

}

//向左转

public void gotoLeft(){

if("E".equals(this.fx)){

this.fx = "N";

} else if("S".equals(this.fx)){

this.fx = "E";

} else if("W".equals(this.fx)){

this.fx = "S";

} else if("N".equals(this.fx)){

this.fx = "W";

}

}

//向右转

public void gotoRight(){

if("E".equals(this.fx)){

this.fx = "S";

} else if("S".equals(this.fx)){

this.fx = "W";

} else if("W".equals(this.fx)){

this.fx = "N";

} else if("N".equals(this.fx)){

this.fx = "E";

}

}

}

如有问题,欢迎指正。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值