简体   繁体   English

方法被调用了8次,我不明白为什么?

[英]Method is being called 8 times and I do not understand why?

My test review is asking "How many times will niceHippo () be called?" 我的测试评论询问“将多次调用niceHippo()?” and the correct answer is 8. I'm having trouble understanding this as no matter how I look at it I'm not seeing how it results in 8. Please help 正确的答案是8。无论我怎么看,我都很难理解,我看不到它在8中的效果如何。请帮助

public class Animals{

    public static String niceHippo()
    {
        String hippo = "Nice Hippo";
        return hippo;
    }

    public static String niceLion(){
        String lion = "Nice Lion";
        return lion;
    }

    public static void main(String[] args){
        int count = 13;
        String stringOut = "I love this class ";
        do
        {
            stringOut = "Animals can be messy ";
            for (int order = 1; order < 5; ++ order)
                for (int copy = 1; copy <= 2; copy++)
                    System.out.println(niceHippo());
            System.out.println(niceLion());
        }while (count != 13);

        count = 13;
        while (count > 10)
        {
            count--;
        }

        System.out.println(stringOut + count);
    }
}

在您的代码中,您要for (int order = 1; order < 5; ++ order)从order = 1到4迭代外循环for (int order = 1; order < 5; ++ order)并且对于每次迭代,将for (int copy = 1; copy <= 2; copy++)复制值for (int copy = 1; copy <= 2; copy++)迭代是1和2以及niceHippo从内环调用,所以作为结果niceHippo正在被呼叫的8倍

For the loop using copy, each time that's ran you print nice hippo twice because copy <=2 Then the loop using order is going to ask that copy loop to run 4 times because order <5 对于使用copy的循环,每次运行时,您都会打印两次漂亮的河马,因为copy <=2然后使用order的循环将要求该copy循环运行4次,因为order <5

so in the end nice hippo prints 8 times because one loop prints nice hippo twice and its asked to do that 4 times by the outer loop. 因此最后,漂亮的河马打印了8次,因为一个循环将漂亮的河马打印了两次,并要求外部循环将其打印4次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的DataPoints方法被多次调用? - Why is my DataPoints method being called multiple times? 使用 Mockito.spy() 模拟被另一个方法调用的方法时,为什么会出现 UnnecessaryStubbingException? - When using Mockito.spy() to mock a method being called by another method, why do I get a UnnecessaryStubbingException? 准备后无法理解为什么未调用register方法的原因。 始终调用customeraction-validation.xml - Not able to understand the why register method is not being called after prepare. always customeraction-validation.xml is being called 如何检查模拟方法被调用了多少次? - How do I check how many times a mocked method was called? 为什么多次调用enableCrashReporting()? - Why is enableCrashReporting() being called multiple times? 为什么没有调用 onCreate() 方法? - Why is onCreate() method not being called? 为什么在打印对象时调用toString()方法? - Why is the toString() method being called when I print an object? 为什么我的方法在System.in.read()循环后被调用3次 - Why my method being called 3 times after System.in.read() in loop 如何检查是否正在调用特定的 class 或 class 的方法? - How do I check if a particular class or a method of a class is being called or not? 为什么在检票口模型中将完成方法称为2次 - Why is finalize method called 2 times in wicket model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM