java面試題之編程【火星車問題】

來源:互聯網
上載者:User

/**
 *author:Kevin
 *date:2011-07-17
 *function:The problem of Mars car.

 *China firmly opposes Obama-Dalai meeting
 *The WAR is actually begun!Now you are the Commander of the Fleet.
 *You  can input command to control the Mars Car to go straight,
 *turn left,turn right,and the car maybe your loyal soldier.
 *The command input like this string:LL1LRR89L2,
 *and at last  report the location of the car to Commander.
 *L means turn left,R means turn right,number0-9 means how far the car can go
 *request:1).ExtremeProgramming 2).Pair Programming 3).Test Driven Development
 */

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;

public class TestMarsCar {

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

  BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
  System.out
    .println("Now,the Mars car is loarding on the Mars,input the loaction");
  System.out.println("input x:(number)");
  String x = bf.readLine();
  System.out.println("input y:(number)");
  String y = bf.readLine();
  System.out.println("input direction:(N,W,S,E)");
  String f = bf.readLine();
  System.out.println("the car is loaded in (" + x + "," + y + "," + f
    + ")");
  // create Marscar object
  Marscar car = new Marscar(x, y, f);

  while (true) {
   System.out.println("\n***********************"
     + "\npress 1or 2 to select *" + "\n1:new command         *"
     + "\n2:exit                *"
     + "\n***********************\ninput number:");
   Scanner s1 = new Scanner(System.in);
   int ints1 = Integer.parseInt(s1.next());
   if (ints1 == 1) {
    System.out.println("input new command like (L,R,0-9):");

    Scanner s = new Scanner(System.in);
    String commands = s.next();
    for (int i = 0; i < commands.length(); i++) {
     String command = String.valueOf(commands.charAt(i));

     if (command.equals("L")) {
      car.turnLeft(car.getF());
     } else if (command.equals("R")) {
      car.goRight(car.getF());
     } else {
      int n = Integer.parseInt(command);
      car.goAhead(n);
     }
     // show the location of mars car.
     System.out.println("the new location is (" + car.getX()
       + "," + car.getY() + "," + car.getF() + ")");

    }
   } else if (ints1 == 2) {
    System.exit(0);
   } else {
    System.out.println("YOu command is Wrong!");
   }
  }
 }
}

// -----------------------------------------
// class Marscar
class Marscar {
 private String x; // x-coordinate
 private String y; // Y-coordinate
 private String f; // direction

 public String getX() {
  return x;
 }

 public void setX(String x) {
  this.x = x;
 }

 public String getY() {
  return y;
 }

 public void setY(String y) {
  this.y = y;
 }

 public String getF() {
  return f;
 }

 public void setF(String f) {
  this.f = f;
 }

 // constructor
 public Marscar(String x, String y, String f) {
  this.x = x;
  this.y = y;
  this.f = f;
 }

 public void turnLeft(String f) {
  System.out.println("go left");
  if (f.equals("N")) {
   setF("W");
  } else if (f.equals("E")) {
   setF("N");
  } else if (f.equals("S")) {
   setF("E");
  } else if (f.equals("W")) {
   setF("S");
  }
 }

 public void goRight(String f) {
  System.out.println("go right");
  if (f.equals("N")) {
   setF("E");
  } else if (f.equals("E")) {
   setF("S");
  } else if (f.equals("S")) {
   setF("W");
  } else if (f.equals("W")) {
   setF("N");
  }
 }

 // input number to control how far the car go ahead
 public void goAhead(int n) {
  int intx = Integer.parseInt(x);
  int inty = Integer.parseInt(y);
  if (this.getF().equals("N")) {
   intx += n;
  } else if (this.getF().equals("E")) {
   inty += n;
  } else if (this.getF().equals("S")) {
   intx -= n;
  } else if (this.getF().equals("W")) {
   inty -= n;
  }
  this.x = String.valueOf(intx);
  this.y = String.valueOf(inty);
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.