我该如何编码一个Java汽车程序,该程序需要字符串并返回整数

时间:2019-07-19 03:03:17

标签: java

我的方法将采用字符串并返回移动数。北(X),南(Y),东(A)或西(B)。例如:“ A”应返回2:在起始位置返回一个,在东方返回一个。 “ XYAB”将在一个正方形中返回4。 XYXYXY在两个方向上移动时将仅返回2。我传递一个字符时无法返回2。我在这里遗漏了什么吗?

下面是我尝试的代码

public class Car {

    public static String removeDuplicates(String str) {
        HashSet<Character> set = new HashSet<Character>();
        char[] chars = str.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            set.add(chars[i]);
        }
        Iterator<Character> iterator = set.iterator();
        String sbString = new String();
        while (iterator.hasNext())
            sbString += iterator.next() + "";
        return sbString;
    }
    static void finalMoveMethod(String value) {
            int l =value.length();
            int North =0, South =0, West =0,  East =0;

            for (int i =0; i < l;i++) {

                if(value.charAt(i) == 'X' )
                    North++;
                else if(value.charAt(i) == 'A' )
                    East++;
                else if(value.charAt(i) == 'B' )
                    West++;
                else if(value.charAt(i) == 'Y' )
                    South++;

            }
            System.out.println("Number of moves : " + (North - South + West- East));
            System.out.println("North : " + North);
            System.out.println("South : " + South);
            System.out.println("West : " + West);
            System.out.println("East : " + East);
        }
public static void main(String[] args) {
        String move = "XYAB";
        String val = removeDuplicates(move);
        finalPosition(val);
    }
}

0 个答案:

没有答案