跳至主要內容

4-while

黑静美原创...大约 1 分钟编程CcppC井

while 语称为循环结构,因为它在一个表达式为真时重复执行语句,不断循环。它的语法为:

while (表达式) {
    循环体语句
}

表达式评估为真或假,执行体代码可以是单一语句,或者由大括号 { } 括起来的代码块。 例如:

    while (guess != number) {
        printf("Please guess a number between 1 and 100.\n");
        scanf("%d", &guess);
        if(guess < number){
            printf("You guess: %d, it's too small. ", guess);
        }else if(guess > number){
            printf("You guess: %d, it's too big. ", guess);
        }
    }
    printf("You guess: %d, it's right.", guess);

猜数游戏

#include <stdio.h>
#include <stdlib.h>


int main() {
    int number,guess;
    printf("Please guess a number between 1 and 100.\n");

    while(!(number >= 1 && number <= 100)) {
        number = rand();
    }

    /*1表示为真,break终止了“真”*/
    while(1){
        scanf("%d", &guess);   //赋值
        while(guess < 1 || guess > 100) {
            printf("The number must be between 1 and 100. Try again: \n");
            scanf("%d", &guess);
        }

        if(guess < number){
            printf("You guess: %d, it's too small.\n", guess);
        }else if(guess > number){
            printf("You guess: %d, it's too big.\n", guess);
        }else{
            printf("You guess: %d, it's right.\n", guess);
            break;
        }
    }
    printf("Game end.\n", guess);
    return 0;
}

练习:阶乘和乘法表

#include <stdio.h>

int main() {
    int a = 1 , b = 1;
    /* the factorial */
    int n;
    printf("Of witch nummer the factorial do you want?\n");
    scanf("%d" , &n);

    while(n < 0){
        printf("It is illegal. Please input a new number.\n");
        scanf("%d" , &n);
    }

    while( b <= n ){
        a *= b;
        b ++ ;
    }
    printf("The factorial of the entered nummer is: %d\n",a);

    /*the multiplikation table*/
    int c = 0;
    a = 0;
    printf("the multiplikation table for n = ?\n");
    scanf("%d" , &n);
    while( c <= n ){
        while (a <= c){
            printf("%d * %d = %d  " , a , c , a*c);
            a ++ ;
        }
        printf("\n" );
        c ++ ;
        a = 1;
    }
    printf("The multiplikation table ends.\n");

    return 0;
}
上次编辑于:
贡献者: Heijingmei
评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v3.1.3