日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java坐标移动题目case_坐标移动

發布時間:2024/10/8 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java坐标移动题目case_坐标移动 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

面向對象的思想 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_坐标移动的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。