c-如何请求输入并将输入转换为满足打印条件的变量



我需要帮助才能使代码正常工作。我希望它能让用户输入一个小于50的数字,如果这是真的,它会打印hello world。否则,终端将要求另一个输入。

#include <cs50.h>
#include <stdio.h>
int main(void)
{
// Asking user for a number less or equal to 50
int i = get_int("Choose a number less or equal to 50n");
// If i is less or equal to 50 print "hello world"
if ((int) i <= 50)
{
printf("hello, worldn");
i++;
}

}

您可以使用goto而不是循环。我已经创建了下一个例子中的函数:

#include <stdio.h>
#define PRINT_N  int n = 10;for(;n--;printf("Hello Worldn")) 
int main(void)
{   
int input_number;
start:
printf("nChoose a number less or equal to 50n");
scanf("%d",&input_number);
if(input_number <= 50 ) 
{
PRINT_N;
}
else
goto start;

return 0;
}

我希望这会有所帮助。

您应该使用循环来重复执行某些操作。

#include <cs50.h>
#include <stdio.h>
int main(void)
{
int i;
for (;;)
{
// Asking user for a number less than 50
i = get_int("Choose a number less than 50n");
// If i is less than 50, print "hello world"
if (i < 50)
{
printf("hello, worldn");
i++;
break; // exit from the loop
}
}
}

相关内容

最新更新