---
title: "java面试题：火星探测器。"
author: "iseesea"
date: "2011-01-05T21:07:01Z"
tags: ["360", "Java", "面试"]
categories: ["编程语言"]
slug: "java"
draft: false
lastmod: "2011-01-05T21:14:08Z"
id: 860045
url: "https://iseesea.iteye.com/blog/860045"
---

前几天收到某公司的面试测试题，自己是做完了。。。但是，。。。。没有得到回复。。。估计是被刷了。。。不过没啥，自己也算是经历过，主要考察面向对象的思想吧；如果有人有更好的方法的话希望可以讨论一下，~~~我发现javaeye上面居然已经有了。。。。。。 \[quote] 一小队机器人探测器将由NASA送上火星高原，探测器将在这个奇特的矩形高原上行驶。 用它们携带的照相机将周围的全景地势图发回到地球。每个探测器的方向和位置将由一个x,y系坐标图和一个表示地理方向的字母表示出来。为了方便导航，平原将被划分为网格状。位置坐标示例：0，0，N，表示探测器在坐标图的左下角，且面朝北方。为控制探测器，NASA会传送一串简单的字母。可能传送的字母为：'L','R'和'M'。 'L',和'R'分别表示使探测器向左、向右旋转90度，但不离开他所在地点。'M' 表示向前开进一个网格的距离，且保持方向不变。假设以广场（高原）的直北方向为y轴的指向。 输入：首先输入的line是坐标图的右上方，假定左下方顶点的坐标为0，0。剩下的要输入的是被分布好的探测器的信息。每个探测器需要输入wo lines。第一条line 提供探测器的位置，第二条是关于这个探测器怎样进行高原探测的一系列说明。位置是由两个整数和一个区分方向的字母组成，对应了探测器的（x,y）坐标和方向。每个探测器的移动将按序完成，即后一个探测器不能在前一个探测器完成移动之前开始移动。 输出：每个探测器的输出应该为它行进到的最终位置坐标和方向。输入和输出 测试如下： 期待的输入: 5 5 1 2 N LMLMLMLMM 3 3 E MMRMMRMRRM 期待的输出 1 3 N 5 1 E \[color=red]尽量少用if...else和switch.\[/color] \[/quote] 坐标：Position.java \[code="java"] /* * To change this template, choose Tools | Templates * and open the template in the editor. \*/ //��� package mars; /\** * * @author Allen \*/ public class Position { private int x; private int y; public Position(String x,String y){ this.x = Integer.parseInt(x); this.y = Integer.parseInt(y); } public void setX(int \_x) { this.x = \_x; } public int getX() { return this.x; } public void setY(int \_y) { this.y = \_y; } public int getY() { return this.y; } @Override public String toString() { return "(" + x + "," + y + ")"; } } \[/code] 方向Direction.java，之所以把移动和转向单独拿出来，是觉得这个东西是共性，比如你面向东方，那你左转一定是北方等等，之类的共性拿出来当一个小类。对于面向对象来说呢，这个就可以理解成探测器上面的方向控制器，喔~只要一个命令，就会计算好移动或者转向之后的位置，然后机器人在移动过去。。。有没有不同理解的？ \[code="java"] //方向控制器，控制转向和移动。 package mars; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; public class Direction { private static Direction instance; private static ArrayList index = new ArrayList(); private static Map direction = new HashMap(); public Direction(){ index.add('N'); index.add('E'); index.add('S'); index.add('W'); direction.put('N', 1); direction.put('E', 1); direction.put('S', -1); direction.put('W', -1); } public static Direction getInstance(){ if (instance == null){ instance = new Direction(); } return instance; } public char turnTo(char faceTo,String LR){ String lr = LR.toUpperCase(); int count = (lr.equals("L") ? -1 : 1 ); char new\_faceTo = index.get((index.indexOf(faceTo) + count + 4)%4); return new\_faceTo; } public Position moveXY(char faceTo,Position pos){ if(index.indexOf(faceTo)%2 == 1){ pos.setX(pos.getX()+direction.get(faceTo)&lt;=0 ? 0 : pos.getX()+direction.get(faceTo) ); }else{ pos.setY(pos.getY()+direction.get(faceTo)&lt;=0 ? 0 : pos.getY()+direction.get(faceTo)); } return pos; } } \[/code] 探测器Detector.java \[code="java"] /* * To change this template, choose Tools | Templates * and open the template in the editor. \*/ package mars; /\** * * @author Allen \*/ public class Detector { private Position pos; private char faceTo; private String moveCmd; public Detector(char faceTo,Position pos,String cmd){ this.pos = pos; this.faceTo = faceTo; this.moveCmd = cmd; } public void excuteCMD(Direction d){ String cmd = this.getMoveCmd(); char\[] cmd\_array = cmd.toUpperCase().toCharArray(); for(int i=0;i cmd = new ArrayList(); //探测器组 List det = new ArrayList(); Scanner sc = new Scanner(System.in); System.out.print("输入‘exit’退出：\\n"); String str = sc.nextLine(); while (!str.toUpperCase().equals("EXIT")) { cmd.add(str); str = sc.nextLine(); } //验证命令，看是否数目准确； if(cmd.size()%2!=1){ System.out.print("请输入正确的命令数量。"); } String\[] maxXY = cmd.get(0).split(" "); Position maxPos = new Position(maxXY\[0].trim(),maxXY\[1].trim()); //初始化探测器，并让他执行命令 for(int i=1;i