[英]Can't seem to figure out why my loop wont progress
I can't seem to figure out why my for loop is not working.我似乎无法弄清楚为什么我的 for 循环不起作用。 I have been on the for about an hour now and the closest I have come is this.
我已经上了大约一个小时了,我最近的一次是这个。 I'm trying to find out how many McNuggets you can buy using the 6, 9, and 20 packs.
我想知道你可以用 6、9 和 20 包购买多少个 McNugget。 All I need dopeChecker(x) to do is return a true or a false.
我需要 dopeChecker(x) 做的就是返回一个真或假。 I haven't implemented check the next number yet because it wont even find that a 6 pack can be bought yet.
我还没有实施检查下一个号码,因为它甚至不会发现可以购买 6 包。 I know it's in the loop somewhere but I just can't find out where.
我知道它在某个地方的循环中,但我就是不知道在哪里。 This is form the MIT open course ware problem 2. I don't know if you guys have seen it but I'm just letting you know this is where I'm getting my info.
这是麻省理工学院开放课件问题 2 的形式。我不知道你们是否看过,但我只是让你们知道这是我获取信息的地方。
int x = 0, y = 0, z = 0;// These will the the pack of McNuggets that we can buy.
int testFor = 0; //This will be the number of McNuggets we are looking for.
int matches = 0; //This will be the number of consecutive matches we will be looking for.
public void dopeEquation(){
while (matches < 6){//It's 6 Because that is the smallest order of nuggets we can buy.
//Looking for smaller nuggets then we can buy would not make sense.
while (testFor < 6){
testFor++;
}
if (dopeChecker(testFor)){
matches++;
} else{
matches = 0;
System.out.println(x + "," + y +"," + z +"," + testFor + "," + matches);
}
}
}
private boolean dopeChecker(int testFor){
for ( x = 0 ; x*6 <= testFor; x++){
for ( y = 0 ; y*9 <= testFor; y++){
for (z = 0 ; z*20 <= testFor;){
System.out.println(x + "," + y +"," + z +"," + testFor + "," + matches);
if (x*6 + y*9 + z*20 == testFor){
matches++;
System.out.println("match");
return true;
}else{
System.out.println("no match");
}
}
}
}
return false;
}
}
The z variable is always 0, you're not changing it. z 变量始终为 0,您无需更改它。
The following is the innermost for loop.以下是最里面的 for 循环。 I have commented where the problem is.
我已经评论了问题出在哪里。 as you can see,
z
never gets implemented.如您所见,
z
永远不会被实现。 therefore, the loop will never terminate, as 0 <= testFor, since TestFor >= 6.因此,循环永远不会终止,因为 0 <= testFor,因为 TestFor >= 6。
for (z = 0 ; z*20 <= testFor;/* incrementor needed here*/){
System.out.println(x + "," + y +"," + z +"," + testFor + "," + matches);
if (x*6 + y*9 + z*20 == testFor){
matches++;
System.out.println("match");
return true;
}else{
System.out.println("no match");
}
}
Your code goes inside the first while loop:您的代码进入第一个 while 循环:
while (matches < 6){
Then increased testFor to 6 with the following code:然后使用以下代码将 testFor 增加到 6:
while (testFor < 6){
testFor++;
}
Then goes to dopeChecker:然后去dopeChecker:
dopeChecker(int testFor)
Then insde the 3rd loop, for z:然后进入第三个循环,对于 z:
for (z = 0 ; z*20 <= testFor;) {
...
}
z is never incremented, so you need to write this as: z 永远不会增加,因此您需要将其写为:
for (z = 0 ; z*20 <= testFor; z++){
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.