My test review is asking "How many times will niceHippo () be called?" 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
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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.