火焰游戏逻辑使用java给出错误结果?

时间:2014-11-26 11:06:59

标签: java string

这里我创建了一个火焰游戏逻辑,它是最终的字符串长度是正确的(例如:4个用于两个字符串'raja'和'rani')基于我需要显示'f'或'l'或'a的长度'或'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.

我们将不胜感激。

5 个答案:

答案 0 :(得分:2)

希望这就是你所需要的。我在这里击中了火焰这个词的特征,直到我得到一个角色。一旦我得到了火焰结果的角色。

 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

答案 1 :(得分:0)

我认为问题在于这一行:

int count = temp - 1;

这会给您一个3的答案,其中包含(0)F (1)L (2)A (3)M

所以改成它:

int count = temp; //- 1;

获取(0)F (1)L (2)A (3)M (4)E

答案 2 :(得分:0)

'/^\s.{9}$/'

答案 3 :(得分:0)

import java.util.Scanner;

    public class Flames {

        public static void main(String[] args) {
            /* Check the flames using two names  */
            Scanner scan = new Scanner(System.in);
            System.out.print("Enter Name 1 : ");
            String Name1 = scan.nextLine();
            //System.out.println(Name1);
            System.out.print("Enter Name 2 : ");
            String Name2 = scan.nextLine();
            //System.out.println(Name2);
            int N1len = Name1.length();
            int N2len = Name2.length();
            Name1 = Name1.toLowerCase();
            Name2 = Name2.toLowerCase();

            StringBuffer NameB1 = new StringBuffer(Name1);
            StringBuffer NameB2 = new StringBuffer(Name2);
            int Name3=0;
            for (int i = 0; i<N1len; i++) 
            {
                for (int j = 0; j<N2len; j++)
                 {
                    if (NameB1.charAt(i) == NameB2.charAt(j)) 
                    {
                        NameB1 = NameB1.deleteCharAt(i);
                        NameB2 = NameB2.deleteCharAt(j);
                        N1len= NameB1.length();
                        N2len= NameB2.length();
                        i=0;
                        j=0;
                    }

                }

            }
            Name3= NameB1.length()+NameB2.length();
            //System.out.println(NameB1);
            //System.out.println(NameB2);
            //System.out.println(Name3);

            /* Flames Calculation */

            char flameResult =0;
            String flames = "flames";
                    StringBuffer sb3 = new StringBuffer(flames);
            while (sb3.length()!=1)
            {
                int Name4 =  Name3%sb3.length();
                String temp;
                if(Name4!=0)
                {
                temp = sb3.substring(Name4)+sb3.substring(0, Name4-1);
                }
                else
                {
                temp=sb3.substring(0,sb3.length()-1);
                }
                sb3 = new StringBuffer(temp);

                        flameResult=sb3.charAt(0);
            }   

            switch(flameResult)
            {
            case 'f':   System.out.println("Friends");
                        break;
                        case 'l':
                            System.out.println("Love");
                        break;
                        case 'a':
                            System.out.println("Affection");
                        break;
                        case 'm':
                            System.out.println("Marriage");
                            break;
                    case 'e':
                        System.out.println("Enemies");
                            break;
                        case 's':
                            System.out.println("Sibling");
                            break;

            }



        }

    }

答案 4 :(得分:0)

import java.util.*;
public class Flames
{
   public static void main()
   {
      int sp=0;
      Scanner sc=new Scanner(System.in);
      System.out.println("enter two names");
      String s=sc.nextLine().toLowerCase();
      String s1=sc.nextLine().toLowerCase();
      String p="flames";
      String p1="flames";
      String s2="";
      String m="";
      for(int i=0;i < s.length();i++)
      {
         if(Character.isLetter(s.charAt(i))) 
         {
            m=m+s.charAt(i);
         }
      }
      s=m;
      m="";
      for(int i=0;i < s1.length();i++)
      {
         if(Character.isLetter(s1.charAt(i))) 
         {
            m=m+s1.charAt(i);
         }
     }
    s1=m;
    m="";
    int l=s.length();
    int l1=s1.length();
    for(int i=0;i < l;i++)
    {
       int sl=0;
       for(int j=0;j < s1.length();j++)
       {
          if(s.charAt(i)==s1.charAt(j))
          {
             if(sl==0)
             {
               sl++;sp++;s2=s2+" " ;
             }
             else
             {
               s2=s2+s1.charAt(j);
             }
          }
          else
          {
            s2=s2+s1.charAt(j);
          }
      }
     s1=s2;
     s2="";
   }
   sp=sp*2;
   int c=(l-1)+(l1-1)-(sp-1);
   for(int i=1;;i++)
   {
      String z="";p=p+p+p+p+p+p+p+p+p+p+p+p+p+p+p+p+p+p+p+p+p+p+p+p+p+p+p+p+p+p+p+p+p;
     String mn="";
     char c1=p.charAt(c);
     for(int j=0;j < p1.length();j++)
     {
        if(c1==p1.charAt(j))
        {
           mn=p1.substring(j+1)+mn;
           break;
        }
        else
        {
          mn=mn+p1.charAt(j);
        }
      }
      for(int k=0;k < p1.length();k++)
      {
         if(c1==p1.charAt(k))
         {
         }
         else
         {
            z=z+p1.charAt(k);
         }
      }
      p1=z;
      p=mn;
      if(mn.length()==1)
      {
         System.out.println(mn);
         break;
      }
    }
  }
}