【问题标题】:Assignment to write a program that gives the user a choice between two options - C分配编写一个程序,让用户在两个选项之间进行选择 - C
【发布时间】: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) 调用与其对应的函数。

标签: c function choice


【解决方案1】:

你应该从用户的键盘上读取输入:

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!");

【讨论】:

    【解决方案2】:

    你应该使用 Switch case。

        switch(choice) {
         case 1: //first for loop
         break;
         case 2: //second for loop
         break;
        }
    

    【讨论】:

    • 这正是我所需要的。非常感谢你。现在一切正常!
    【解决方案3】:

    看起来您在这里遗漏了几点。首先,您还没有编写任何函数。尝试查看here 以了解这方面的一些信息。

    其次,要根据用户输入做出选择,您需要以某种方式实际获取该输入。你可能想使用scanf

    最后,一旦您获得了用户的输入(例如,在声明为int input; 的变量中),您就可以使用if 根据该变量控制程序的流程,如下所示:

    if(input == 1){
        greet();
    }
    else {
        countDown();
    }
    

    干杯!如果您还有任何问题,请随时在下方发表评论。

    【讨论】:

    • 谢谢!那条线很有帮助。
    • @MissAnnThropic 我很高兴。不过,您应该实现我和其他建议的功能,否则您将无法获得全部功劳:)
    【解决方案4】:

    首先,你实际上并没有声明你的函数。 C 中的函数应该像 main 函数那样声明。如需了解更多信息,请参阅here

    // display greeting and name 4 times
    void greeting(){
        for(i=1;i<=4;i++)
        {
            printf("Hi, my name is Bridget\n");
        }
    }
    
    void countdown() {
    // display countdown
        for(i=10;i>=0;--i)
        {
            printf("%d\n", i);
        }
            printf("Blastoff!");
    }
    

    获取用户输入最常用的方法是通过键盘。 scanf 在 C 中实现了这一点。scanf 的详细信息here

    int main(void){
      int i, choice;
    
      //prompt the user to make a choice
      // You don't need 2 printf for the newlines stick them to one.
      printf("What would you like to do?\n  1. Print my name\n  2. Count down from 10\n\n");
    
      //This takes the user's input and puts it in the variable choice
      scanf(%d, &choice);
    }
    

    最后,要根据用户输入决定做什么,您可以使用if then else 语句或switch。我将提供一个带有if 语句的解决方案,您可以自己计算带有switch 的解决方案。您的最终代码应如下所示。

    int main(void){
        int i, choice;
    
        //prompt the user to make a choice
        // You don't need 2 printf for the newlines stick them to one.
        printf("What would you like to do?\n  1. Print my name\n  2. Count down from 10\n\n");
    
        //This takes the user's input and puts it in the variable choice
        scanf(%d, &choice);
    
        if(choice == 1){
          greeting();
        }else{
          countdown();
        }
    }
    
    // display greeting and name 4 times
    void greeting(){
        for(i=1;i<=4;i++)
        {
            printf("Hi, my name is Bridget\n");
        }
    }
    
    void countdown() {
    // display countdown
        for(i=10;i>=0;--i)
        {
            printf("%d\n", i);
        }
            printf("Blastoff!");
    } 
    

    请记住,这段代码有很多缺陷(主要是错误检查),但我猜你的分配与此无关。

    【讨论】:

      【解决方案5】:

      首先,您需要包含具有您需要的功能的库。你这样做是

      #include <someLibrary.h>
      

      在您的文档开始时。库大多具有 .h 扩展名。如果您尝试做某事,请始终寻找它们。您认为它们具有尽可能好的性能和功能(并非总是如此)。

      接下来你要声明你的函数。函数具有名称、进入其中的参数、它们执行某些操作的主体和返回值(可以是浮点数、整数、字符等)。如果函数没有返回任何内容,则返回 void(最后没有 return)。您在 main() 之前声明函数,只有参数类型。整个身体在主要之后(更好看)。

      如果你用参数声明函数,你必须在 () 括号中提供这些参数给函数。即使不需要参数,您也可以像下面示例中的 getch() 一样使用它们。请注意,函数成为它返回的内容。如果您在函数中声明了一些新变量,它们将仅在函数中可见。另一方面,函数将看不到来自其他函数的任何变量(main 也是)。如果需要,请声明全局变量(不推荐)。

      #include <stdio.h>
      #include <conio.h>  //libraries
      
      void function1(int);
      float function2(float);  //declaration of functions
      
      int main()
      {   
      char decision;
      printf("press 'a' to run function1, press 'b' to run function2\n");
      decision=getch();     //first see getch()? look in google for functionality and library !
          int someInt=10;
          float someFloat=11;
      
           if(decision== 'a')
          {
           function1(someInt);
          }
          else if(decision == 'b')
          {
           printf("%f", funcion2(someFloat));  //example that function become what    they return
          }
          else 
          {
           printf("No decision has been made");
          }
      getch();   //program will wait for any key press
      return 0;
      }
      
      void function1(int param1)
      {
        //print your stuff               // this function return void, so doesnt have return; statement
      } 
      float function2(float param1)
      {
       return 2*param1;                 //this function have to return some float
      }
      

      【讨论】:

        猜你喜欢
        • 2013-02-23
        • 2013-09-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-09
        • 2022-08-20
        相关资源
        最近更新 更多