【问题标题】:C programming do while with switch case programC 编程 do while 与 switch case 程序
【发布时间】:2014-09-26 16:34:10
【问题描述】:

我已经能够切换案例程序,但我希望程序一次又一次地运行,直到用户选择退出。 我基本上希望程序使用 do while 循环一次又一次地运行......

switch(I)
{
case 1:
    printf("67");
    break;
case 2:
    printf("45");
    break;
default:
    printf("default");
}

【问题讨论】:

  • 好的,太好了!有什么不适合的?你用的是什么代码?这实际上是用哪种语言编写的? C# 和 Objective-C 非常不同。
  • 有可能,调查一下万能的Infinite-Loop

标签: c


【解决方案1】:

像这样使用do...while 循环:

int I = 1; //Initialize to some non-zero number to prevent UB
printf("Enter 0 to quit \n");
do{
    if (scanf("%d",&I) != 1) //If invalid data such as characters are inputted
    {
        scanf("%*[^\n]");
        scanf("%*c");    //Clear the stdin
    }
} while(I!=0); //Loop until `I` is not 0 

这段代码会一直循环,直到用户输入0。你可以根据自己的需要改变这段代码。如果您想要您的switch,请在scanf 之后复制您发布的代码。

【讨论】:

    【解决方案2】:

    循环将一直运行,直到您输入 -1 作为输入。

    #include<stdio.h>
    int main()
    {
        int I;
        do
        {
            puts("Enter -1 to quit");
            printf("Enter your choice: ");
            scanf("%d",&I);
            switch(I)
            {
                case 1:
                printf("67\n");
                break;
                case 2:
                printf("45\n");
                break;
                case -1:
                puts("Bye");
                break;
                default:
                printf("default\n");
            }
        }while(I != -1);
        return 0;
    }
    

    【讨论】:

      【解决方案3】:

      这个程序一直运行到用户输入 0 或负数...

      #include<stdio.h>
      
      int main()
      {
          int I;
          do
          {
            scanf("%d",&I);
            switch(I)
            {
              case 1:
              printf("67");
              break;
              case 2:
              printf("45");
              break;
              default:
              printf("default");
            }
          }
          while(I>0);
              return 0;
      }
      

      【讨论】:

        【解决方案4】:

        Do-While 循环的简单使用。

        Choice 是存储用户选择的变量,无论他是否想再次打印该语句。

        int choice;
        do{
           printf("\nHello World!");  //This is the task of the program (Replace it with your task)
           printf("\nDo You Want to Print it again ? 1 Yes/0 No: ");
           scanf("%d",&choice);
        }while(choice==1); //Loop will exit when choice gets value other than 1
        

        【讨论】:

          【解决方案5】:
          // here switch will run until A is not equal to S
          
          
          int N; 
          char A;
          
          do{
          cin>>N;
          N = N%7;
          cout<<endl;
          cin>>A;
          
          switch(N)
              {
                  case 1: cout<<"Monday"<<endl; break;   
                  case 2: cout<<"Tuesday"<<endl; break; 
                  case 3: cout<<"Wednesday"<<endl; break;
                  case 4: cout<<"Thursday"<<endl; break;
                  case 5: cout<<"Friday"<<endl; break;
                  case 6: cout<<"Saturaday"<<endl; break;
                  case 0: cout<<"Sunday"<<endl; break;
                  
                  default: cout<<"Invalid Input"; }}
          
          
          while(A!='S'); 
          

          【讨论】:

            猜你喜欢
            • 2021-10-01
            • 1970-01-01
            • 2020-05-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多