在这里我创建了一个flames游戏逻辑,它基于需要显示“f”或“l”或“a”或“f”的长度来确定最终的字符串长度是正确的(例如:两个字符串“raja”和“rani”为4) m'或'e'或's'。
我已经根据字符可见的长度写了逻辑,但这不是我所关心的。对于长度4的结果应该是'e'(在长度4的火焰中,首先是'm'应该去掉然后'l'然后是'f'然后是'a'然后是's'最后我需要'e'作为输出。有人可以告诉我想法,这是我的代码。

public static void main(String[] args) {
    String name1 = "raja";
    String name2 = "rani";
    String s1 = name1;
    String s2 = name2;
    for (int i = 0; i < name1.length(); i++) {
        for (int j = 0; j < name2.length(); j++) {
            if (name1.charAt(i) == name2.charAt(j)) {
            name1 = name1.replaceFirst(String.valueOf(name1.charAt(i)), "#");
            name2 = name2.replaceFirst(String.valueOf(name2.charAt(j)), "#");
            }
        }
    }
    String result = name1 + name2;
    result = result.replaceAll("#", "");
    int resultLength = result.length();
    String baseInput = "flames";
    char relationIs = 0;
    int temp = 0;
    if (resultLength > 0) {
        temp = resultLength % baseInput.length();
    }
    if (temp == 0 && resultLength >= 6) {
        relationIs = 's';
    } else {
        int count = temp - 1;
        if (count >= 0) {
            relationIs = baseInput.charAt(count);
        System.out.println("Relation Betw " + s1 + " and " + s2 + " is:");
        }
    }
    switch (relationIs) {
        case 'f':
            System.out.println("friendship");
            break;
        case 'l':
            System.out.println("Lovers");
            break;
        case 'a':
            System.out.println("Affection");
            break;
        case 'm':
            System.out.println("Marriage");
            break;
        case 'e':
            System.out.println("Enemity");
            break;
        case 's':
            System.out.println("Siblings");
            break;
        default:
            System.out.println("FLAME Test works only for different names");
            break;
    }
}

逻辑遵循此顺序:仅向前方向删除4个字符。
if length=4
step 0:flames ('f' as 1)
step 1:flaes (here 'e' as 1)
step 2:faes  (here 'a' as 1)
step 3:aes   (here 'a' as 1)
step 4:es    (here 'e' as 1)
step 5:e   //output.

您的帮助将不胜感激。

最佳答案

希望这是您所需要的。我在这里敲打火焰一词的字符,直到我得到一个字符。一旦我得到了那是火焰的结果的角色。

 if (resultLength > 0) {
    while (baseInput.length() !=1)
        {
           System.out.println(baseInput);
           int tmpLen = resultLength % baseInput.length(); //finding char position to strike
           if(tmpLen != 0)
           {
               temp = baseInput.substring(tmpLen) + baseInput.substring(0, tmpLen-1); //Append part start from next char to strike and first charater to char before strike.
           }
           else
           {
               temp = baseInput.substring(0, baseInput.length()-1); //If mod result zero we can strike last letter easily
           }
           baseInput = temp; //Assign the temp to baseinput for next iteration.
        }
        relationIs = baseInput.charAt(0);
        System.out.println(relationIs);
 }

连结:http://ideone.com/Fqgcc1

关于java - 火焰游戏逻辑使用java给出错误结果?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27147538/

10-10 00:45