gpt4 book ai didi

c - 这个变量的值是如何增加的? - 代码 objective-c

转载 作者:太空宇宙 更新时间:2023-11-04 03:29:11 25 4
gpt4 key购买 nike

我目前正在从一本书的教程中学习 C,我正在遵循此代码。

我不明白的是变量 numberOfBottles 是如何在这一行从 1 开始并以 4 结束的。感谢大家的帮助,我对 Objective C 还是个新手

printf("Put a bottle in the recycling, %d empty bottles in the bin.\n", numberOfBottles);


void singSongFor(int numberOfBottles)
{
if (numberOfBottles == 0) {
printf("There are simply no more bottles of beer on the wall.\n\n");
} else {

printf("%d bottles of beer on the wall. %d bottles of beer.\n", numberOfBottles, numberOfBottles);
int oneFewer = numberOfBottles - 1;

printf("Take one down, pass it around, %d bottles of beer on the wall.\n\n", oneFewer);
singSongFor(oneFewer); //This function calls itself!

// Print a mesasge just before the function ends

printf("Put a bottle in the recycling, %d empty bottles in the bin.\n", numberOfBottles);
}
}

int main(int argc, const char * argv[])
{
// We could sing 99 verses, ut 4 is easier to think about
singSongFor(4);
return 0;
}

最佳答案

设置断点并尝试理解此代码段的执行,您将了解它是如何工作的。

当我尝试这段代码时,它的执行方式如下:

4 bottles of beer on the wall. 4 bottles of beer.
Take one down, pass it around, 3 bottles of beer on the wall.

3 bottles of beer on the wall. 3 bottles of beer.
Take one down, pass it around, 2 bottles of beer on the wall.

2 bottles of beer on the wall. 2 bottles of beer.
Take one down, pass it around, 1 bottles of beer on the wall.

1 bottles of beer on the wall. 1 bottles of beer.
Take one down, pass it around, 0 bottles of beer on the wall.

There are simply no more bottles of beer on the wall.

Put a bottle in the recycling, 1 empty bottles in the bin.
Put a bottle in the recycling, 2 empty bottles in the bin.
Put a bottle in the recycling, 3 empty bottles in the bin.
Put a bottle in the recycling, 4 empty bottles in the bin.

关于c - 这个变量的值是如何增加的? - 代码 objective-c ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38496853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com