栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > Java面试题

java面试

Java面试题 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java面试


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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/359832.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号