Flames game logic using java giving error result?

Here I created the logic of playing with flame, this is the correct line length is correct (ex: 4 for two lines "raja" and "rani") based on the length I need to show "f" or "l" or "a 'or' m 'or 'e' or 's'. I wrote logic based on the length of this character, but that's none of my business. For length 4, the result should be "e" (in a flame for length 4, first "m" should remove "l", then "f" then "a" then "s" finally I need "e" as output Someone tell me This is my code.

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;
    }
}

      

The logic follows this order: only the forward direction removes 4 characters.

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.

      

Your help would be appreciated.

+3


source to share


5 answers


Hope this is what you need. I am here hitting the character of the flame word until I get one character. Once I get the symbol, it is the result of the flame.

 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);
 }

      



Link: http://ideone.com/Fqgcc1

+2


source


I think the problem is on this line:

int count = temp - 1;

      

Which gives you the answer 3

that gives(0)F (1)L (2)A (3)M



So change it to:

int count = temp; //- 1;

      

To obtain (0)F (1)L (2)A (3)M (4)E

0


source


    // Another Right Code to Find FLAMES in JAVA 
    // It is simple
    import java.util.Scanner;
class Flames
{
 public static void main(String ar[])
 {
  Scanner sc=new Scanner(System.in);
  String name,fname,flm="flames";
  System.out.println("Enter the boy name...");
  name=sc.next();
  System.out.println("Enter the girl name...");
  fname=sc.next();
  int l=name.length();
  int gl=fname.length();
  int num=0,tl=0; 
  char n[]=name.toCharArray();
  char gn[]=fname.toCharArray();
  for(int i=0;i<l;i++)
  {  
   for(int j=0;j<gl;j++)
   {
    if(n[i]==gn[j])
    {
     n[i]='*';
     gn[j]='*';
     break;
    }
   }
  }
  String tname=new String(n);
  tname=tname+(new String(gn));
  tname=tname.replace("*","");
  tl=tname.length();
  System.out.println(tl);

  for(int s=6;s>=2;s--)
  {
   if(tl>s)
     num=tl-s;
   else
     num=tl;
   while(num>s)
   {
     num=num-s;
   } 
   flm=flm.substring(num,flm.length())+(flm.substring(0,num-1)); 
  }
  switch(flm.charAt(0))
  {
   case 'f': System.out.println("Relationship = Friends"); break;
   case 'l': System.out.println("Relationship = Lovers"); break;
   case 'a': System.out.println("Relationship = Affections"); break;
   case 'm': System.out.println("Relationship = Married"); break;
   case 'e': System.out.println("Relationship = Enemy"); break;
   case 's': System.out.println("Relationship = Brother & Sisters"); break;
  }
  //System.out.println("Name ="+flm);
 }
}

      

0


source


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;

            }



        }

    }

      

0


source


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;
      }
    }
  }
}

      

0


source







All Articles