Java程序计算机器人的朝向及其坐标值

Java程序计算机器人的朝向及其坐标值,java,arrays,Java,Arrays,我有一个5*5的矩阵,最初机器人的坐标(2,2)是朝北的。 机器人向左、右、上、下、左的方向移动。 我必须计算机器人运动后的坐标(LRUDL)及其最终朝向 下面是我尝试过的代码,但我无法在其中获得机器人方向。 如果有人能在这件事上帮我,那就太好了 在这里输入代码 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class FinalPositi

我有一个5*5的矩阵,最初机器人的坐标(2,2)是朝北的。 机器人向左、右、上、下、左的方向移动。 我必须计算机器人运动后的坐标(LRUDL)及其最终朝向

下面是我尝试过的代码,但我无法在其中获得机器人方向。 如果有人能在这件事上帮我,那就太好了

在这里输入代码

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class FinalPosition {

    public static void main(String[] args) throws NumberFormatException, IOException {

        BufferedReader br1=new BufferedReader(new InputStreamReader(System.in)); 
        System.out.println("Enter the moving direction:");
        String move = br1.readLine();
        //String move = "LRUDL"; 
        position(move); 

    }

    private static void position(String move) throws NumberFormatException, IOException {

        BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 
        System.out.println("Enter the X axis co ordinates:");
        int xaxis = Integer.parseInt(br.readLine());
        System.out.println("Enter the Y axis co ordinates:");
        int yaxis = Integer.parseInt(br.readLine());
        System.out.println("Enter the initial direction of the robot");
        String direction = br.readLine(); 

        int l = move.length();
        int countUp = 0, countDown = 0; 
        int countLeft = 0, countRight = 0; 
        int x =0;
        int y= 0;
        int count= 0;

        char [] c=move.toCharArray();
        System.out.println(c);

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

            if(c[i]== 'U')
                countUp++; 
            else if (c[i] == 'D') 
                countDown++; 
            else if (c[i] == 'L') 
                countLeft++; 
            else if (c[i] == 'R') 
                countRight++; 
            else 
                count = 0;
        }

        if(countRight >= countLeft){
             x = (countRight - countLeft);
        } else {
             x = (countLeft - countRight);
        }

        if(countUp >= countDown ){
            y = (countUp - countDown);
        } else {
            y = (countDown - countUp);
        } 

        System.out.println("X AXIS IS"+   x   + "  Y AXIS IS  "+ y);
        System.out.println("xaxis "+ xaxis   + "  yaxis  "+ yaxis);
        int finalXaxisPosition = xaxis - x;
        int finalYaxisPosition = yaxis - y;
        System.out.println("Final Position: "+ finalXaxisPosition + "," + finalYaxisPosition);
    }

}
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStreamReader;
公共类最终位置{
公共静态void main(字符串[]args)引发NumberFormatException,IOException{
BufferedReader br1=新的BufferedReader(新的InputStreamReader(System.in));
System.out.println(“输入移动方向:”);
字符串move=br1.readLine();
//字符串move=“LRUDL”;
位置(移动);
}
私有静态无效位置(字符串移动)引发NumberFormatException,IOException{
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
System.out.println(“输入X轴坐标:”);
int xaxis=Integer.parseInt(br.readLine());
System.out.println(“输入Y轴坐标:”);
int yaxis=Integer.parseInt(br.readLine());
System.out.println(“输入机器人的初始方向”);
字符串方向=br.readLine();
int l=move.length();
int countUp=0,countDown=0;
int countLeft=0,countRight=0;
int x=0;
int y=0;
整数计数=0;
char[]c=move.toCharArray();
系统输出打印ln(c);
for(int i=0;i=countLeft){
x=(countRight-countLeft);
}否则{
x=(countLeft-countRight);
}
如果(倒计时>=倒计时){
y=(倒计时-倒计时);
}否则{
y=(倒计时-倒计时);
} 
System.out.println(“X轴为“+X+”Y轴为“+Y”);
System.out.println(“xaxis”+xaxis+“yaxis”+yaxis);
int finalXaxisPosition=xaxis-x;
int finalYaxisPosition=yaxis-y;
System.out.println(“最终位置:“+finalaxisposition+”,“+finalaxisposition”);
}
}

您可能想得太多了

你面对的最终方向就是你的最后一步。例如,无论你做什么动作,如果你最后一步是向左,你的机器人总是面向西方

String direction = "";
int lastIndex = move.length - 1;
switch(c[lastIndex]) {
    case 'L': direction = "West"; break;
    case 'U': direction = "North"; break;
    case 'D': direction = "South"; break;
    case 'R': direction = "East"; break;
    default: throw new IllegalArgumentException("Cannot accept movement " + c[lastIndex]);
}
System.out.println("Final Direction: " + direction);

你使用了什么输入,它给了你什么输出,它应该给你什么输出?我用2,2输入了“LRUDL”,结果是1,2,我觉得这是正确的。我不确定你有什么问题。我想知道机器人移动后的最终方向输入:输入移动方向:LRUDL输入X轴坐标:2输入Y轴坐标:2输出:最终位置:1,2最初机器人在移动(LRUDL)后朝北我想知道它的朝向。欢迎来到Stack Overflow!请阅读“如何创建一个应用程序”。然后使用链接改进您的问题(不要通过评论添加更多信息)。否则,我们无法回答您的问题并帮助您。