c# 当我输入1时,我的交换机机箱不工作我的机箱不工作

shstlldc  于 2022-12-02  发布在  C#
关注(0)|答案(1)|浏览(173)

当我运行代码时,我的开关情况不起作用,当我输入1时,它不打印情况1,等等,我尝试了每件事,但没有工作,有人能帮助我吗?当我运行代码时,我的开关情况不起作用,当我输入1时,它不打印情况1,等等,我尝试了每件事,但没有工作,有人能帮助我吗?

#include <stdio.h>
  #include <math.h>
   int main ()
  {
       int choice;
       float area;

   printf("1 for area of Square\n");
    printf(" 2 for area of Circle\n");
   printf(" 3 for finding area of rectangle\n");

    switch(choice) {

     case 1: {
         float side,area;
          printf("Enter Sides of Square");
          scanf("%f",&side);
             area=(float)side*side;
             printf("Area of Square is %f",area);
            break;
           }
  
  
         case 2: {
            float radius,area;
          printf("Enter Radius of Circle");
           scanf("%f",&radius);
            area=(float)3.14159*radius*radius;
           printf("Area of Circle %f",area);
           break;
          }
           case 3: {
           float len,breadth,area;
           printf("Enter Length and Breadth of Rectangle");
            scanf("%f %f",&len,&breadth);
              area=(float)len*breadth;
           printf("Area of Rectangle is %f",area);
            break;
              }
 
          default: {
           printf("Invalid Choice");
           break;
          }

       }
       return 0;**strong text**
      }
kognpnkq

kognpnkq1#

您缺少用户输入。我已经在行下面添加了scanf,它现在可以工作了

scanf("%d", &choice);

代码完整版本

#include <stdio.h>
#include <math.h>
int main ()
{
    int choice;
    float area;

    printf ("1 for area of Square\n");
    printf (" 2 for area of Circle\n");
    printf (" 3 for finding area of rectangle\n");
    scanf("%d", &choice);

    switch (choice)
    {
        case 1:
        {
            float side, area;
            printf ("Enter Sides of Square");
            scanf ("%f", &side);
            area = (float) side *side;
            printf ("Area of Square is %f", area);
            break;
        }
        case 2:
        {
            float radius, area;
            printf ("Enter Radius of Circle");
            scanf ("%f", &radius);
            area = (float) 3.14159 *radius * radius;
            printf ("Area of Circle %f", area);
            break;
        }
        case 3:
        {
            float len, breadth, area;
            printf ("Enter Length and Breadth of Rectangle");
            scanf ("%f %f", &len, &breadth);
            area = (float) len *breadth;
            printf ("Area of Rectangle is %f", area);
            break;
        }
        default:
        {
            printf ("Invalid Choice");
            break;
        }
    }
  return 0;
}

输出量:

相关问题