现在的位置: 首页 > 综合 > 正文

停车场Java考题,无聊时可以看看

2013年08月13日 ⁄ 综合 ⁄ 共 4908字 ⁄ 字号 评论关闭
问题三:
 模拟汽车在某个停车场内的活动。
 假设有某个5x5的停车场,横轴为-2到+2,纵轴为-2到+2,一辆汽车停在中心(0,0),面向上方(称之为北方)。 

用户的输入有三种:前进一格(F),原地左转90度(L),原地右转90度(R)。
根据用户的输入序列,模拟汽车的活动。
 
程序结束的条件是, 
 1)当汽车超过点阵范围后,认为汽车掉入沟中(FALL)而不能动弹了,程序不再处理输入,结束; 
2)输入(S)后结束。
 
每一步动作后给出最终汽车所处的位置以及方向(用N,S,E,W表示北、南、东、西)
 
编程要求: 用你认为合适的面向对象方式用java代码描述以上过程,并且能接受输入,得出结果。
输入可以用任何你认为方便的方式进行,例如stdin,读入文件,或者用某种方法固定写在某个启动类中,每次运行前修改亦可。不必检查输入的合法性。
 
每一步都输出在stdout中。格式如下:输入内容,结果横坐标,纵坐标,方向
 
例子1:这是一个前进一步,左转两次,再前进到头的例子,
输入为:FLLFFFFFFFF
START,0,0,N
F,0,1,N
L,0,1,W
L,0,1,S
F,0,0,S
F,0,-1,S
F,0,-2,S
F,0,-3,S
FALL
 
例子2:输入:FRFRFRFRS
START,0,0,N
F,0,1,N
R,0,1,E
F,1,1,W
R,1,1,S
F,1,0,S
R,1,0,W
F,0,0,W
R,0,0,N
STOP
 
---------------------------------------------------------------------------------------
 
package cxg;
public class Car {
 private int stationX = 0;
 private int stationY = 0;
 private int direction = 1;
 
 public int getDirection() {
  return direction;
 }
 public void setDirection(int direction) {
  this.direction = direction;
 }
 public int getStationX() {
  return stationX;
 }
 public void setStationX(int stationX) {
  this.stationX = stationX;
 }
 public int getStationY() {
  return stationY;
 }
 public void setStationY(int stationY) {
  this.stationY = stationY;
 }
 
}
 
--------------------------------------------------------------------------------
 
package cxg;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class CoreProcess {
 
 private final int maxX = 2;
 private final int minX = -2;
 private final int maxY = 2;
 private final int minY = -2;
 
 private int nowX = 0;
 private int nowY = 0;
 private int directionNum;
 
 
 //N:1   E:2  S:3  W:4
 private final int[] dirNum = {1,2,3,4};
 private final char[] direction  = {'N','E','S','W'};
 private final char[] control = {'F','L','R'};
 private final char exit = 'S';
 //当前的VO,保存上一步的状态
 private Car tempVO = new Car();
 
 /*
  * stackMap{InfoVO}
  */
 private List stateList = new ArrayList();
 
 public static void main(String[] args){
  CoreProcess instance = new CoreProcess();
  instance.getInput();
  instance.viewPrint();
 }
 
 private void getInput(){
  try {
         BufferedReader stdIn = new BufferedReader(
                 new InputStreamReader(System.in));
         String userInput;
         userInput = stdIn.readLine();
      //while ((userInput = stdIn.readLine()) != null) {
        System.out.println("Custom input : " + userInput);
        System.out.println("START ,0,0,N");
        processInput(userInput);
      //}
      stdIn.close();
       
         }catch(IOException e){
          System.err.println("Couldn't get I/O .");
          System.exit(1);
         }
 }
 
 //处理控制台显示
 private void viewPrint(){
  //如果利用JDK1.5autoboxing 等处理更好
  Iterator ite = stateList.iterator();
  while(ite.hasNext()){
   Car vo = (Car)ite.next();
   //这里如果用jdk1.5printf()处理更方便些
   System.out.println(vo.getDirection()+","+vo.getStationX()+","+vo.getStationY());
  }
 }
 
 private boolean checkIsFall(int x,int y){
  boolean flag = true;
  if(x > maxX || x < minX){
   flag = false;
  }
  if(y > maxY || y < minY){
   flag = false;
  }
  //增加处理汽车掉坑里{1,1}{1,-1}
  if( (x == 1 && y == 1) || (x == 1 && y == -1) ) {
   flag = false;
   System.out.println("This Car has turn over at { x= "+x+",y= "+y+" }");
  }
  return flag;
 }
 
 private void processInput(String input){
  //没有考虑大小写
  char inChar;
  int len = input.length();
  for(int i=0;i<len;i++){
   inChar = input.charAt(i);
   if( !operation(inChar) ){
    break;
   }
  }
 }
 
 private boolean operation(char inChar){
  boolean flag = true;
  Car thisVO = new Car();
  
  //处理退出
  if(inChar == exit){
   System.exit(0);
  }
  
  //处理方向
  else if(inChar == control[1] || inChar == control[2]){
   directionNum = tempVO.getDirection();
   if(inChar == control[2]){
    directionNum = (directionNum + 1) % 4 ;
    doSetDirection(directionNum,thisVO);
   }else{
    if(directionNum > 1){
     directionNum = (directionNum - 1) % 4 ;
     doSetDirection(directionNum,thisVO);
    }else if(directionNum == 1){
     directionNum = 4;
     doSetDirection(directionNum,thisVO);
    }
   }
   
  //处理前进 
  }else if(inChar == control[0]){//F
   nowX = tempVO.getStationX();
   nowY = tempVO.getStationY();
   int fangxiang = tempVO.getDirection();
   
   if(fangxiang == dirNum[0]){//N:1
    nowY++;
    tempVO.setStationY(nowY);
    flag = doSetAllProperties(nowX,nowY,tempVO.getDirection(),thisVO);
   }
   else if(fangxiang == dirNum[1]){//E:2
    nowX++;
    tempVO.setStationX(nowX);
    flag = doSetAllProperties(nowX,nowY,tempVO.getDirection(),thisVO);
   }
   else if(fangxiang == dirNum[2]){//S:3
    nowY--;
    tempVO.setStationY(nowY);
    flag = doSetAllProperties(nowX,nowY,tempVO.getDirection(),thisVO);
   }
   else if(fangxiang == dirNum[3]){//W:3
    nowX--;
    tempVO.setStationX(nowX);
    flag = doSetAllProperties(nowX,nowY,tempVO.getDirection(),thisVO);
   }
   
  }//处理前进 end
  
  return true;
 }
 
 
 private void doSetDirection(int direction,Car vo){
  tempVO.setDirection(direction);
  vo.setDirection(direction);
  vo.setStationX(tempVO.getStationX());
  vo.setStationY(tempVO.getStationY());
  stateList.add(vo);
 }
 
 private boolean doSetAllProperties(int x,int y,int direction,Car vo){
  if(!checkIsFall(nowX,nowY)){
   return false;
  }
  vo.setDirection(direction);
  vo.setStationX(x);
  vo.setStationY(y);
  stateList.add(vo);
  
  return true;
 }
 
}
 

抱歉!评论已关闭.