我有一项作业要到期,但我对到底要做什么却一片空白……我确信这很简单,但我还没有完全掌握窍门。作业是 -
编写一个程序,为用户提供 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!");
}
最佳答案
您应该从用户键盘读取输入:
int c;
c = getchar();
if (c == '1')
{
// display greeting and name 4 times
for(i=1;i<=4;i++)
{
printf("Hi, my name is Bridget\n");
}
}
if (c == '2')
{
// display countdown
for(i=10;i>=0;--i)
{
printf("%d\n", i);
}
}
printf("Blastoff!");
关于c - 分配编写一个程序,让用户在两个选项之间进行选择 - C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40639482/