如何让控制台不断提示用户输入,直到用户在C中输入一个正整数? #包括 #包括 内部主(空) { printf(“以分钟为单位告诉我您淋浴的时间:\n”); intx=GetInt(); int b=x*12; 如果(x

如何让控制台不断提示用户输入,直到用户在C中输入一个正整数? #包括 #包括 内部主(空) { printf(“以分钟为单位告诉我您淋浴的时间:\n”); intx=GetInt(); int b=x*12; 如果(x,c,cs50,C,Cs50,您需要“循环”来重复代码,在c中,有for,while,do while。例如: #include <stdio.h> #include <cs50.h> int main(void) { printf("Give me the length of your shower in minutes: \n"); int x = GetInt(); int b = x*12; if (x <= 0) {

您需要“循环”来重复代码,在c中,有
for
while
do while
。例如:

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

int main(void)
{   
    printf("Give me the length of your shower in minutes: \n");
    int x = GetInt();
    int b = x*12;

    if (x <= 0)
        {
        printf("Please give me a valid input.\n");   
        }

    else           
        printf("In %i minutes you consumed %i bottles of water!\n", x, b);
}
while(true){
x=GetInt();
如果(x其中“做点别的”意味着也离开循环:p

while (true) {
    x = GetInt();
    if (x <= 0) {
        printf("Please give me a valid input.\n");
    } else {
        // do something else
    }
}
while(true){
x=GetInt();

如果(x以下内容将重复提示,直到输入有效值,然后打印结果:

while (true) {
    x = GetInt();
    if (x <= 0) {
        printf("Please give me a valid input.\n");
    } else {
        // do something else
        // and leave
        break;

    }
}
int main(无效)
{   
int x=0;
做
{
printf(“以分钟为单位告诉我您淋浴的时间:\n”);
x=GetInt();

}while(x)您需要使用
while
循环。
int main(void)
{   
    int x = 0 ;

    do
    {
        printf( "Give me the length of your shower in minutes:\n" ) ;
        x = GetInt() ;

    } while( x <= 0 ) ;

    int b = x * 12 ;
    printf( "In %i minutes you consumed %i bottles of water!\n", x, b ) ;

    return 0 ;
}