面向对象的思想 package com.special.spet;

import java.util.Scanner;

/**

*

* @author special

* @date 2017年9月6日 下午5:35:09

*/

class Point{

private int x;

private int y;

private static final char[] INSTRUCT_LETTER = {'A', 'S', 'W', 'D'};

public int getX(){

return x;

}

public int getY(){

return y;

}

private static boolean contain(char ch){

for(char item : INSTRUCT_LETTER)

if(item == ch)

return true;

return false;

}

private static boolean isNum(char ch){

if(ch >= '0' && ch <= '9') return true;

else return false;

}

public static boolean isStandard(String instruct){

if(instruct.length() > 3 || !contain(instruct.charAt(0))) { return false; }

for(int i = 1; i < instruct.length(); i++)

if(!isNum(instruct.charAt(i)))

return false;

return true;

}

/**

* 处理每一个指令,包括判断是否合格

* [@param instruct

* @return](/profile/547241) 一个待移动量的Point对象

*/

public static Point prestationOfInstruct(String instruct){

Point target = new Point();

if(!isStandard(instruct)) {return target;}

int steps = Integer.parseInt(instruct.substring(1));

switch(instruct.charAt(0)){

case 'A': target.x = -steps; break;

case 'S': target.y = -steps; break;

case 'W': target.y = steps; break;

case 'D': target.x = steps; break;

}

return target;

}

/**

* 根据参数的移动量的target来进行相加(减法其实也是一种加法)

* @param target

*/

public void move(Point target){

this.x += target.x;

this.y += target.y;

} [@Override](/profile/992988) public String toString(){

return x + "," + y;

}

}

public class Pro16 {

public static void main(String[] args){

Scanner input = new Scanner(System.in);

while(input.hasNext()){

String str = input.nextLine();

String[] instructs = str.split(";");

Point origin = new Point();

for(String instruct : instructs)

origin.move(Point.prestationOfInstruct(instruct));

System.out.println(origin);

}

}

}

进一步优化: package com.special.spet;

import java.util.Scanner;

/**

*

* @author special

* @date 2017年9月6日 下午10:05:48

*/

class Point{

private int x;

private int y;

public int getX(){

return x;

}

public int getY(){

return y;

}

public void setX(int x) {

this.x = x;

}

public void setY(int y) {

this.y = y;

}

@Override

public String toString(){

return x + "," + y;

}

}

public class Pro16Improve {

/**

* 利用空间存储指令的方向移量

*/

private static final String INSTRUCT_LETTER = "ASWD";

private static final int[] distenceX = {-1, 0, 0, 1};

private static final int[] distenceY = {0, -1, 1, 0};

private static int find(char ch){

for(int i = 0; i < INSTRUCT_LETTER.length(); i++){

if(ch == INSTRUCT_LETTER.charAt(i))

return i;

}

return -1;

}

private static boolean isNum(char ch){

if(ch >= '0' && ch <= '9') return true;

else return false;

}

public static void move(Point point, String instruct){

if(instruct.equals("") || instruct.length() > 3 || find(instruct.charAt(0)) == -1) { return ;}

int x = 0;

int y = 0;

for(int i = 1; i < instruct.length(); i++){

if(isNum(instruct.charAt(i))){

// x,y上的移量为倍数乘以单位移量

x = x * 10 + (instruct.charAt(i) - '0') * distenceX[find(instruct.charAt(0))];

y = y * 10 + (instruct.charAt(i) - '0') * distenceY[find(instruct.charAt(0))];

}

else return;

}

point.setX(point.getX() + x);

point.setY(point.getY() + y);

}

public static void main(String[] args){

Scanner input = new Scanner(System.in);

while(input.hasNext()){

String str = input.nextLine();

String[] instructs = str.split(";");

Point origin = new Point();

for(String instruct : instructs)

move(origin, instruct);

System.out.println(origin);

}

}

}

java坐标移动题目case_坐标移动相关推荐

  1. java坐标移动题目case_用java怎样编写一个二维坐标平移程序

    展开全部 java编写二维坐标平移程序,主要是e69da5e887aa62616964757a686964616f31333337626231通过类继承Point2D,使用里面的方法来平移,如下代码: ...

  2. java获取屏幕上某坐标点的颜色

    全栈工程师开发手册 (作者:栾鹏) java教程全解 java获取屏幕上某坐标点的颜色.通过先获取屏幕截图,再获取坐标点颜色. public Color getScreenPixel(int x, i ...

  3. Java计算两个GPS坐标点之间的距离(可用于计算里程等)

    直接贴上工具类: public class PositionUtil {private static final double EARTH_RADIUS = 6378.137; // 6378.137 ...

  4. JAVA使用JTS 判断坐标点是否在坐标多边形内部

    JAVA使用JTS 判断坐标点是否在坐标多边形内部 思路 Geometry之间的关系 API及参考博客 代码 依赖 工具类 测试类 思路 判断坐标点是否在坐标多边形内部,首先不能直接计算坐标点,是需要 ...

  5. java 经纬度坐标转换 WGS84、火星坐标 (GCJ-02)、百度坐标 (BD-09)

    会有偏移,但是还能接受 WGS84 国际标准,从 GPS 设备中取出的数据的坐标系 国际地图提供商使用的坐标系 火星坐标 (GCJ-02) 中国标准,从国行移动设备中定位获取的坐标数据使用这个坐标系 ...

  6. Java程序设计编程题目

    Java程序设计编程题目 一般题: 1. 编写一个应用程序,对程序中给定的四个double型数据求其最大值和最小值. import java.util.*; public class no1 { pu ...

  7. Java课程设计题目一:动物换位

    Java课程设计题目一:动物换位 1 设计要求 设计GUI界面的动物换位游戏,游戏结果是让左.右两组动物交换位置.具 体要求如下: ①在水平排列的7个位置上左.右各有3个类型相同的动物,中间的位置上没 ...

  8. 百度地图墨卡托坐标转高德经纬度坐标(偏移小)

    基本上是网上常见的方法进行坐标系的转换,但是误差很大.发现之所以误差大是在于百度的墨卡托坐标转百度的经纬度时误差太大,后面找到一个方法,误差较小,基本吻合. 参考:http://www.site-di ...

  9. php 经纬度坐标转换 WGS84、火星坐标 (GCJ-02)、百度坐标 (BD-09)

    项目有gps上报的功能, 由于前端插件问题导致大量gps定位数据转换百度坐标(BD-09)时产生极大偏移, 故需要后端做经纬度坐标转换, 看到一篇java的相关技术帖, 拿来做了修改 Ps: 坐标转换 ...

最新文章

  1. 杂记2:VS2013创建Windows服务实现自动发送邮件
  2. python中可以用中文作为变量-在Python 3.x中可以使用中文作为变量名。
  3. ogm session_带有Hibernate OGM的NoSQL –第一部分:持久化您的第一个实体
  4. (转)MVC模式参数传递的探究
  5. redhat挂载镜像软件包
  6. 应用传送网络(ADN):率先架起“东数西算”的“高速公路”
  7. 一、Asp.Net Core WebAPI——修改默认监听端口
  8. java object转map_Java 面试题:百度前 200 页都在这里
  9. 初步认识泊松重建(比较全的综合教程)
  10. [机器学习实战]决策树
  11. 信号检测与估计理论_校对招募 | 信号检测论的贝叶斯估计
  12. 【p2p】【EdgeVPNio (evio)】简介: IP-over-P2P (IPOP)
  13. “跟着吴恩达老师入门机器学习”学习笔记(二)
  14. 智能硬件无线通信协议(二)
  15. 【Pix4d精品教程】Pix4Dmapper完整航测内业操作流程手把手图文教程
  16. fftshift有什么用?MATLAB做FFT后为什么还要fftshift?
  17. 关于股票除权复权,前复权、后复权、不复权
  18. 阿拉丁和神灯的故事(二)
  19. GPFS各类排故日志收集汇总
  20. TPA3255 classD 音频功放快速设计

热门文章

  1. 【报表技术】IReport图形化报表开发工具生成PDF文档
  2. mysql t添加注释_mysql—添加注释(comment)的用法
  3. Linux下Poppler源码编译安装
  4. SVM支持向量机【直观理解】
  5. python json包_python编程 之 json包
  6. linux-2.6.29内核配置、编译与安装
  7. python搜索路径顺序_Python module之搜索路径
  8. 项目中用到的语音识别方案 硬件/软件相关介绍
  9. 直播预告 | 如何在有限数据下实现资讯类网站海量信息自动分类
  10. 图像分类算法_图像分类算法优化技巧:Bag of Tricks for Image Classification