简体   繁体   English

似乎无法弄清楚为什么我的循环不会进展

[英]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.

相关问题 您好,我似乎无法弄清楚为什么我的程序无法正常工作 - Hello I can't seem to figure out why my program isn't working Java的新手,学习速度很快,但无法弄清楚为什么我的for循环不会打印 - New to Java, learning quick, but can't figure out why my for loops wont print 似乎无法弄清楚为什么冒泡排序在 Java 中不起作用 - Can't seem to figure out why bubble sort is not working in java 我正在使用Servlet,但似乎无法弄清楚为什么我的下载文件Servlet可以在本地运行,但不能正常运行 - I'm using Servlets and can't seem to figure out why my download file servlet will work locally, but not otherwise 无法弄清楚For Loop - Can't figure out For Loop 我似乎无法弄清楚如何处理此实例,以便我的代码可以打印出要求 - I can't seem to figure out what to do with this instance so that my code could print out the requirement Android Java:无法弄清楚如何循环我的VideoView - Android Java: Can't figure out how to loop my VideoView 似乎无法弄清楚如何忽略循环中最左侧和最右侧的数组元素 - Can't seem to figure out how to ignore left and right most array elements in loop 我似乎无法弄清楚如何将答案显示为计算器项目的单词? - I can't seem to figure out how to display answers as words for my Calculator Project? Java-javax.json-编译错误:.getValueType()的“找不到符号”-似乎找不到原因 - Java - javax.json - Compile Error: “cannot find symbol” for .getValueType() - Can't seem to figure out why
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM