开发者

Where is the infinite loop?

Hello this is my first message in overflow(Sorry if the question is too long) and im also junior at java and english.

I've recently read a mind game. Question was something like this: There is four women,and their names: kirmizi - yesil -sari -mavi (these are colour names in Turkish) Each woman wears a skirt in one these colours. For example;

mavi woman wears sari skirt

but there are two rules:

None of the women's skirt colour should not be same as their name

also the length of their names and skirts cant be equal. For example, mavi woman can't wear sari skirt. But she can wear kirmizi and yesil.

Can you find each woman's skirt colour?

And I've tried to solve it but it goes into infinite loop

Colour=renk name=isim in turkish:)

Edit: I've just found out new clues. Miss kirmizi and sari can't wear mavi and yesil skirts. Also their skirts are different colour. I can write the answer if you want but I believe this is a good problem for beginners like me :)

package ana;

class Ana {

   static boolean birtobir=false;
   static boolean ikitoiki=false;
   static boolean uctouc=false;
   static boolean dorttodort=false;

   String renk;
   String isim;
   public static void main(String[] args) {

        String[] isimler={"bir","iki","uc","dort"};
        String[] renkler={"kirmizi","sari","yesil","mavi"};

        Ana bir = new Ana();
        bir.isim = "kirmizi";
        bir.renk="kirmizi";

        Ana iki = new Ana();
        iki.isim = "sari";
        iki.renk="sari";

        Ana uc = new Ana();
        uc.isim = "yesil";
        uc.renk="yesil";

        Ana dort = new Ana();
        dort.isim = "mavi";
        dort.renk="mavi";

        while ( bi开发者_如何学运维rtobir=true && bir.renk.matches(bir.isim))
               while( ikitoiki=true && iki.renk.matches(iki.isim) )
                      while( uctouc=true && uc.renk.matches(uc.isim) )
                              while( dorttodort=true && dort.renk.matches(dort.isim)) {

                                    for (int a=0;a<renkler.length;a++) {
                                        bir.renk=renkler[a];

                                        if(bir.renk.length()==bir.isim.length()) {
                                            boolean birtobir=true;
                                        }

                                        for (int b=0;b<renkler.length;b++) {
                                            iki.renk=renkler[b];

                                            if(iki.renk.length()==iki.isim.length()) {
                                                boolean ikitoiki=true;
                                            }

                                            for (int c=0;c<renkler.length;c++) {
                                                uc.renk=renkler[c];

                                                if(uc.renk.length()==uc.isim.length()) {
                                                boolean uctouc=true;

                                                for (int d=0;d<renkler.length;d++) {
                                                     dort.renk=renkler[d];

                                                }
                                                if(dort.renk.length()==dort.isim.length()){
                                                     boolean dorttodort=true;
                                                }

                                            }
                                        }   
                                    }
                               }                        
     } 


    System.out.println(bir.isim+"="+bir.renk);
    System.out.println(iki.isim+"="+iki.renk);
    System.out.println(uc.isim+"="+uc.renk);
    System.out.println(dort.isim+"="+dort.renk);


    }
}


It's just a guess, but in here:

while ( birtobir=true && bir.renk.matches(bir.isim))
               while( ikitoiki=true && iki.renk.matches(iki.isim) )
                      while( uctouc=true && uc.renk.matches(uc.isim) )
                              while( dorttodort=true && dort.renk.matches(dort.isim)){

most likely you want: birtobir==true && .. ikitoiki==true && ... uctouc==true && ... dorttodort==true [use operator== instead of the operator=]

birtobir=true sets birtobir to be true, while birtobir==true checks if the variable is true.

more details on operators in java

If it is not a bug, and you actually want to set the vaeriable - it's a bad styling and you should refactor your code.

EDIT: One more thing I can notice:

   if(bir.renk.length()==bir.isim.length()){
            boolean birtobir=true;
        }

In here, you create a new variable named birtobir, and set it to true. This variable is NOT the variable checked [or supposed to be checked] in the while loop. I assume you actually want to set the class member to true. To do so, replace boolean birtobir=true; with birtobir=true;


A single = assigns a value to a variable, while == checks for equality. This means you should change the conditions in your while loop to check for equality instead of assigning a value.

I.E. while ( birtobir=true && bir.renk.matches(bir.isim)) is effectively the same as while (bir.renk.matches(bir.isim)). It should therefore be changed into while ( birtobir==true && bir.renk.matches(bir.isim))

edit: what amit says..


Here's my rendition. If I understand correctly you want to print all the valid combinations?

public class Colours
{
  private static String[] names = new String[]{"kirmizi","yesil", "sari", "mavi"};
  private static String[] colors = new String[]{"kirmizi","yesil", "sari", "mavi"};


  public static void main(String[] args)
  {
    for (int i = 0; i < names.length; i++)
    {
      for (int j = 0; j < colors.length; j++)
      {
        if(allowed(names[i], colors[j])) {
          System.out.println(names[i] + " may wear " + colors[j]);
        }
      }
    }
  }

  private static boolean allowed(String name, String color) {
    return (!name.equals(color)) && (name.length() != color.length());
  }
}


Can't tell for sure what's going on, but you seem to have to many cycles.

I'd try to start with a naive but simple algorithm:

for (person in persons) {
    for (color in skirtColors) {
        if (person.name != color && person.name.lenght != color.lenght) {
            person.skirtColor = color;
        } else {
            person.skirtColor = "";
        }
    }

    if (allPersonsHaveSkirts(persons)) {
        // a more complete check should be done
        // to make sure they all have different colored skirts
        printSolution();
    }
}

By using 2 simple, finite for loops, you guarantee that no infinite loops happen.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜