早教吧 育儿知识 作业答案 考试题库 百科 知识分享

谁能编一个有关C语言选择语句的例子?

题目详情
谁能编一个有关C语言选择语句的例子?
▼优质解答
答案和解析

/*

 *Name: random_day.c

 *Time: 2012-6-17 23:45:45

 *Dscp: 随机产生一个数字代表即将前进的一个方向.

 */

#include <stdio.h>

#include <time.h>

#include <string.h>

#include <stdlib.h>

int main()

{

    int val;

    char drct[5];

    srand(time(0));                     //初始化随机数种子

    val = rand() % 5;                   //产生随机数,并以5为模

    switch(val)

    {

        case 1:

            strcpy(drct, "north");

            break;

        case 2:

            strcpy(drct, "east");

            break;

        case 3:

            strcpy(drct, "south");

            break;

        case 4:

            strcpy(drct, "west");

            break;

        default:

            strcpy(drct, "stay");

            break;

    }

    printf("Now I will: %s\n", drct);

    return 0;

}

看了 谁能编一个有关C语言选择语句...的网友还看了以下: