JAVA机器人响应的代码 java机器人编程
跪求大神荣光 回答下面的机器人的JAVA代码
import java.text.DecimalFormat;
创新互联公司主要从事成都网站建设、成都网站设计、网页设计、企业做网站、公司建网站等业务。立足成都服务孙吴,十载网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18980820575
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class WalkDistance {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
for (int i = 0; i n; i++) {
String order = in.next();
System.out.println(new DecimalFormat("0.00").format(calDistance(order)));
}
in.close();
}
private static float calDistance(String order) {
char[] orderChar = order.toCharArray();
ListString orderList = new ArrayListString();
int num = 0;
for (int i = 0, len = orderChar.length; i len; i++) {
if (orderChar[i] = '0' orderChar[i] = '9') {
num++;
} else {
if (num == 0) {
orderList.add(String.valueOf(orderChar[i]));
} else {
StringBuffer sb = new StringBuffer();
for (int j = num; j 0; j--) {
sb.append(String.valueOf(orderChar[i - j]));
}
orderList.add(sb.toString());
orderList.add(String.valueOf(orderChar[i]));
num = 0;
}
}
if (i == len - 1 num != 0) {
StringBuffer sb = new StringBuffer();
for (int j = num - 1; j = 0; j--) {
sb.append(String.valueOf(orderChar[i - j]));
}
orderList.add(sb.toString());
}
}
Point curPoint = new Point(0, 0, 90);
for (int i = 0, len = orderList.size(); i len; i++) {
if ("R".equals(orderList.get(i))) {
curPoint.angle = (curPoint.angle - 90 + 360) % 360;
} else if ("L".equals(orderList.get(i))) {
curPoint.angle = (curPoint.angle + 90) % 360;
} else {
curPoint.setLocation(curPoint, orderList.get(i));
}
}
return curPoint.getDis();
}
static class Point {
private int x;
private int y;
private int angle;
private float dis;
public Point(int x, int y, int angle) {
this.x = x;
this.y = y;
this.angle = angle;
this.dis = (float) 0;
}
public void setLocation(Point point, String distance) {
if (point.angle == 0) {
point.x += Integer.valueOf(distance);
} else if (point.angle == 90) {
point.y += Integer.valueOf(distance);
} else if (point.angle == 180) {
point.x -= Integer.valueOf(distance);
} else {
point.y -= Integer.valueOf(distance);
}
}
public float getDis() {
return (float) Math.sqrt(this.x * this.x + this.y * this.y);
}
}
}
用java程序实现
机器人类(源类):
public class Robot {
public void cry(){
System.out.println("机器人叫");
}
public void run(){
System.out.println("机器人跑");
}
}
狗类(目标类):
public class Dog {
public void cry(){
System.out.println("像狗一样叫");
}
public void run(){
System.out.println("像狗一样跑");
}
}
适配器类(继承源类并重写它的方法,目标类作为属性):
public class Adaper extends Robot{
private Dog dog;
public Adaper(Dog dog) {
this.dog=dog;
}
@Override
public void cry() {
dog.cry();
}
@Override
public void run() {
dog.run();
}
}
测试类
public class Test {
public static void main(String[] args) {
Dog dog=new Dog();
Robot robot=new Adaper(dog);
robot.cry();
robot.run();
}
}
java什么是响应代码?
这个不是java有的东西.这是http协议的东西.
http简单的说,包括request和response.
request就是请求,包括请求头,请求参数,请求地址等...
response就是响应.同样包括头,参数,还有一个就是响应码.
响应码代表这次的响应大概是什么结果.(只是大概,并一定准确)
而且响应码超级多,大概的有:
404:告诉你,这个地址不存在.
500:告诉你,我这出bug了.
200:一切顺利,请求成功.
太多太多了...你可以查响应码的解释.
一般情况下,响应码是准确的,但我为啥说不一定准确呢.因为对方给你返回响应时,他可以指定响应码.(有些人喜欢修改http协议的默认设置.比如有些奇葩就想把404理解成响应成功.他就可以设置404.或者他设置2019都可以.)
当前标题:JAVA机器人响应的代码 java机器人编程
浏览路径:http://ybzwz.com/article/ddgcpos.html