无限循环在哪里?

发布于 2024-12-07 06:09:52 字数 3336 浏览 0 评论 0原文

你好,这是我在溢出中的第一条消息(抱歉,如果问题太长),我在java和英语方面也是初级的。

我最近读了一本智力游戏。问题是这样的: 有四个女人,她们的名字:kirmizi - yesil -sari -mavi (这些是土耳其语的颜色名称)每个女人都穿着这些颜色之一的裙子。例如;

mavi 女人穿纱丽裙

,但有两个规则:

女人的裙子颜色不能与名字相同

,名字和裙子的长度不能相等。例如,mavi 女人不能穿纱丽裙。但她可以穿 kirmizi 和 yesil。

你能找到每个女人的裙子颜色吗?

我试图解决它,但它进入无限循环

Colour=renk name=isim 土耳其语:)

编辑:我刚刚发现了新的线索。 kirmizi 和 sari 小姐不能穿 mavi 和 yesil 裙子。而且她们的裙子颜色也不同。如果你愿意,我可以写下答案,但我相信这对于像我这样的初学者来说是一个好问题:)

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 ( 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)) {

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


    }
}

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 ( 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)) {

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


    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

巴黎盛开的樱花 2024-12-14 06:09:52

这只是一个猜测,但在这里:

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)){

很可能您想要: birtobi==true && .. ikitoiki==true && ... uctouc==true && ... dorttodort==true [使用 operator== 而不是 operator=]

birtobir=true 将 birtobir 设置为true,而 birtobi==true 检查变量是否为 true。

有关 java 中的运算符的更多详细信息

如果这不是一个错误,并且您实际上想要设置 vaeriable - 这是一个糟糕的样式,您应该重构您的代码。

编辑:
我还注意到一件事:

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

在这里,您创建一个名为 birtobir 的新变量,并将其设置为 true。该变量不是 while 循环中检查[或应该检查]的变量。我假设您实际上想将类成员设置为 true。为此,请将 boolean birtoir=true; 替换为 birtoir=true;

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;

哥,最终变帅啦 2024-12-14 06:09:52

单个 = 为变量赋值,而 == 检查相等性。这意味着您应该更改 while 循环中的条件来检查相等性,而不是分配值。

IE
while ( birtoir=true && bir.renk.matches(bir.isim)) 实际上与 while (bir.renk.matches(bir.isim)) 相同代码>.
因此,应该将其更改为 while ( birtobi==true && bir.renk.matches(bir.isim))

编辑:amit 所说的..

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..

寂寞陪衬 2024-12-14 06:09:52

这是我的演绎。如果我理解正确的话,你想打印所有有效的组合吗?

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

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());
  }
}
太阳男子 2024-12-14 06:09:52

无法确定发生了什么,但你似乎有很多周期。

我尝试从一个简单但简单的算法开始:

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

通过使用 2 个简单的有限 for 循环,可以保证不会发生无限循环。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文