【发布时间】:2019-03-20 11:45:28
【问题描述】:
机器人可以在平面上沿 4 个方向移动:U - 上,D - 下,L - 左,R - 右。流量示例是 UUDLR、DLRUD。我必须实现“walk”方法,以便在通过指定路径后返回机器人位置。所以我这样做:
class Main {
static int[] walk(String path) {
int[] A = {0,0};
char charFromPath;
for (int i = 0 ; i < path.length() ; i++) {
charFromPath = path.charAt(i);
if(charFromPath == 'U')
{
A[1]++;
}
if(charFromPath == 'D')
{
A[1]--;
}
if(charFromPath == 'L')
{
A[0]--;
}
if(charFromPath == 'R')
{
A[0]++;
}
}
return A;
}
public static void main(String[] args) {
{
String path="UUDLR";
int[] position = walk(path);
System.out.println("case 1 - path "+path+ " position 0,1");
if(position[0]==0 && position[1]==1)
System.out.println("the robot went right");
else
System.out.println("the robot smashed");
}
{
String path="DLRUD";
int[] position = walk(path);
System.out.println("\ncase 2 - path "+path+ " position 0,-1");
if(position[0]==0 && position[1]==-1)
System.out.println("the robot went right");
else
System.out.println("the robot smashed");
}
}}
现在在版本 2 中,根据逻辑 U、D、L、R,命令是 UP DOWN LEFT RIGHT。流量示例是 UPUPLEFTRIGHTUP。
现在在 版本 3 中,命令例如 3xUP 2xLEFT DOWN RIGHT。 3xUP 表示向上移动 3 次,2xLEFT 向左移动 2 次。为机器人添加移动限制,他不能离开 10x10 区域(项目 10.10 或 -10,-10 是最后一个有效值)。
我不知道怎么写。如何计算字符串中字符串的重复次数?
【问题讨论】:
-
检查答案,并告诉我您是否需要更多帮助以及它是否能回答您的问题。
-
不是很懂 :( 。这个功能怎么做?
标签: java string find counting word