首页 > 编程语言 >java面试题之编程【火星车问题】

java面试题之编程【火星车问题】

时间:2023-01-15 10:35:19浏览次数:35  
标签:面试题 java String car System equals else public 火星车

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

标签:面试题,java,String,car,System,equals,else,public,火星车
From: https://blog.51cto.com/u_9427273/6008300

相关文章

  • JavaScript while 语句
    while语句可以在某个条件表达式为真的前提下,循环执行指定的一段代码,直到那个表达式不为真时结束循环。——MDNwhile语句也是一种循环语句,也称while循环。while循环接......
  • Java 线程内存模型
    1.前言本节内容是从操作系统的层面谈并发,本节课程我们需要掌握如下内容:了解Java的内存模型定义,是Java并发编程基本原理的基础知识;从概念上了解线程的私有内存空间和主......
  • Java Keytool 介绍
    keytool是Java自带的一个安全相关的工具,用于管理密钥和证书;本文主要介绍其基本使用;文中所使用到的软件版本:Java1.8.0_321。1、简介keytool命令是一个密钥和证书管理......
  • java:Cassandra入门与实战——上
    一、数据存储方式和NoSQL1.1数据存储方式互联网时代各种数据存储方式层出不穷,有传统的关系性数据库如:MySQL、Oracle等,;有全文检索框架如:ElasticSearch、Solr;有NoSQL如:Cassan......
  • Java学习:ribbon的常用负载均衡算法分析
    1.Ribbon介绍因为微服务是目前互联网公司比较流行的架构,所以spring就提供了一个顶级框架-springcloud,来解决我们在开发微服务架构中遇到的各种各样的问题,今天的主角是sprin......
  • Java教程学习:揭秘什么是面向接口编程
    先用一个案例来给大家说明一下面向接口编程。案例:有一个电脑类(Computer),电脑除了有基本的开机关机功能外,还有连接任何外接设备的功能,比如能电脑能连接外置键盘(Keyboard),鼠标......
  • java基础教程:IO流
    一:IO流的分类1)按流向分:输入流:读取数据,把持久设备的数据读取到内存中。输出流:写出数据,把内存的数据写出到持久设备。2)按数据类型分:计算机中一切数据都是:字节数据。字符数据:底......
  • Java基础教程:ArrayList入门
    1ArrayList类概述什么是集合提供一种存储空间可变的存储模型,存储的数据容量可以发生改变ArrayList集合的特点底层是数组实现的,长度可以变化泛型的使用用于约束集合中存储......
  • java:Redis持久化
    一.redis持久化的介绍Redis的持久化指的是将内存中redis数据库运行的数据,写到硬盘文件上。Redis持久化的意义主要在于故障恢复,比如你部署一个Redis,作为缓存有可能里边有......
  • JavaScript 中搜索数组元素的四种方法
    在实际开发当中,我们经常会遇到类似诸如下面的需求:获取满足特定条件的数组中的所有项目要检查是否满足条件?检查数组中是否有特定值?在数组中找到指定值的索引?在本文中,我们将讨......