无限循环在哪里?
你好,这是我在溢出中的第一条消息(抱歉,如果问题太长),我在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这只是一个猜测,但在这里:
很可能您想要:
birtobi==true && .. ikitoiki==true && ... uctouc==true && ... dorttodort==true
[使用 operator== 而不是 operator=]birtobir=true
将 birtobir 设置为true,而birtobi==true
检查变量是否为 true。有关 java 中的运算符的更多详细信息
如果这不是一个错误,并且您实际上想要设置 vaeriable - 这是一个糟糕的样式,您应该重构您的代码。
编辑:
我还注意到一件事:
在这里,您创建一个名为 birtobir 的新变量,并将其设置为 true。该变量不是
while
循环中检查[或应该检查]的变量。我假设您实际上想将类成员设置为 true。为此,请将boolean birtoir=true;
替换为birtoir=true;
It's just a guess, but in here:
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, whilebirtobir==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:
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, replaceboolean birtobir=true;
withbirtobir=true;
单个
=
为变量赋值,而==
检查相等性。这意味着您应该更改 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 aswhile (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?
无法确定发生了什么,但你似乎有很多周期。
我尝试从一个简单但简单的算法开始:
通过使用 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:
By using 2 simple, finite
for
loops, you guarantee that no infinite loops happen.