【发布时间】:2016-11-16 18:22:29
【问题描述】:
我有一个作业到期,但我对具体做什么一无所知...我确信这很简单,但我还没有完全掌握事情的窍门。任务是-
编写一个程序,为用户提供 2 个菜单选项:要么调用一个函数来打印 4 次问候语和你的名字,要么调用一个从 10 倒数到 0 的函数,然后打印“Blastoff!”。这两个函数都应该使用 for 循环来打印适当的输出。
到目前为止,我已经完成了提示和功能......但我不确定如何根据用户的选择显示其中一个或另一个。感谢您的帮助。
#include <stdio.h>
int main (void){
// declare counter variable
int i;
// prompt the user to make a choice
printf("What would you like to do?\n 1. Print my name\n 2. Count down from 10\n");
printf("\n");
// display greeting and name 4 times
for(i=1;i<=4;i++)
{
printf("Hi, my name is Bridget\n");
}
// display countdown
for(i=10;i>=0;--i)
{
printf("%d\n", i);
}
printf("Blastoff!");
}
【问题讨论】:
-
您将需要
scanf用于用户输入,并根据该输入if(userInput == 1)调用与其对应的函数。