gpt4 book ai didi

c - 如何编写正确的 do..while 循环?

转载 作者:行者123 更新时间:2023-11-30 19:04:09 25 4
gpt4 key购买 nike

由于某种原因,当我尝试编译该代码时,它会抛出错误。怎么了,你能告诉我吗?我正在学习 CS50 类(class),这实际上是第一个作业。

程序必须提示用户输入,直到条件为假为止。

#include <stdio.h>
#include <cs50.h>

int n;

do
{
n = get_int();
}
while ( n < 0 || n > 23 );

这是错误:

pyramid.c:6:1: error: expected identifier or '('
do
^
pyramid.c:10:1: error: expected identifier or '('
while ( n < 0 || n > 23 );

最佳答案

这是在 C 中使用 do {...} while(); 循环的通用示例。cs50不是标准的C头文件,这是为参加cs50类(class)的学生自制的。

您应该检查get_int();定义在cs50.h头文件中。

代码:

#include <stdio.h>
int main()
{
int number;

// Do while loop is executed at least once
do
{
printf("Enter a number from 0-23: ");
scanf("%d", &number);

}
while(number < 0 || number > 23);

printf("Number = %d\n",number);

return 0;
}

关于c - 如何编写正确的 do..while 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52703987/

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