java坐标移动题目case-凯发k8官方网
面向对象的思想 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);
}
}
}
总结
以上是凯发k8官方网为你收集整理的java坐标移动题目case_坐标移动的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: java 静态变量 存储_java学习笔
- 下一篇: