微信

使用微信服务,更方便

职友集>Java面试题 > java面试-编程【地球太危险,快回火星去】

java面试-编程【地球太危险,快回火星去】

2015-05-03 06:30:01 阅读( 82 )

1749人 收藏本页

标签:Java面试题

/**
*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);
}
}

来自IT公司面试手册

下一篇:java面试题之多线程

上一篇:java面试题及答案

亲~ 如果您有更好的答案 可在评论区发表您独到的见解。

您想查看更多的信息: 面试题