C#-如何获取字符串并将其转换为布尔表达式,以便用户可以选择选项

C#-如何获取字符串并将其转换为布尔表达式,以便用户可以选择选项,c#,C#,这是在c#中作为控制台应用程序进行编程的。 在我的程序中,我打算询问用户,他们是想要求长方体的面积和体积,圆的面积和周长,还是球体的面积和体积。我已经列出了面积、体积和周长的算法,唯一的问题是我不知道如何询问用户他们想要做哪一个(长方体、圆和球体),然后只运行特定的算法。这可能不是最好的解决方案,但你可以简单地编写“请输入c-圈,v-音量,然后检查用户写了什么 if (input.ToLower().Contains("circle") { //do what you wont t

这是在c#中作为控制台应用程序进行编程的。
在我的程序中,我打算询问用户,他们是想要求长方体的面积和体积,圆的面积和周长,还是球体的面积和体积。我已经列出了面积、体积和周长的算法,唯一的问题是我不知道如何询问用户他们想要做哪一个(长方体、圆和球体),然后只运行特定的算法。

这可能不是最好的解决方案,但你可以简单地编写“请输入c-圈,v-音量,然后检查用户写了什么

 if (input.ToLower().Contains("circle")
 {
     //do what you wont to do.
 }

这可能不是最好的解决方案,但你可以简单地写“请输入c-圈,v-卷,然后检查用户写了什么

 if (input.ToLower().Contains("circle")
 {
     //do what you wont to do.
 }

试着问用户喜欢什么

  • …长方体
  • …圆圈
  • …球体
  • 然后从键盘上读取字符

    char ch = (char)Console.Read();
    
    然后像这样使用switch语句

    switch(ch)
    {
        case '1': { /* insert cuboid algorhytm here */ break;}
        case '2': { /* insert circle algorhytm here */ break;}
        case '3': { /* insert sphere algorhytm here */ break;}
        default : { /* insert invalid selection message */ break;}
    }
    

    试着问用户喜欢什么

  • …长方体
  • …圆圈
  • …球体
  • 然后从键盘上读取字符

    char ch = (char)Console.Read();
    
    然后像这样使用switch语句

    switch(ch)
    {
        case '1': { /* insert cuboid algorhytm here */ break;}
        case '2': { /* insert circle algorhytm here */ break;}
        case '3': { /* insert sphere algorhytm here */ break;}
        default : { /* insert invalid selection message */ break;}
    }
    

    您通常会使用
    Console
    类写入控制台并从控制台读取输入。下面是一个示例程序,可以让您开始:

    class Program
    {
        enum Choice { Volume, Area}
        static void Main(string[] args)
        {
            string input;
            Choice choice;
            do
            {
                Console.WriteLine("1. Volume");
                Console.WriteLine("2. Area");
                Console.Write("Please make your choice: ");
                input = Console.ReadLine();
            } while (!Enum.TryParse(input, out choice));
    
            Console.WriteLine(choice);
    
            switch (choice)
            {
                case Choice.Volume:
                    // calculate volume
                    break;
                case Choice.Area:
                    // calculate area
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }
    }
    

    您通常会使用
    Console
    类写入控制台并从控制台读取输入。下面是一个示例程序,可以让您开始:

    class Program
    {
        enum Choice { Volume, Area}
        static void Main(string[] args)
        {
            string input;
            Choice choice;
            do
            {
                Console.WriteLine("1. Volume");
                Console.WriteLine("2. Area");
                Console.Write("Please make your choice: ");
                input = Console.ReadLine();
            } while (!Enum.TryParse(input, out choice));
    
            Console.WriteLine(choice);
    
            switch (choice)
            {
                case Choice.Volume:
                    // calculate volume
                    break;
                case Choice.Area:
                    // calculate area
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }
    }
    

    考虑到这是一个控制台应用程序,当您作为选项用户时,要求他们输入1或2,并相应地采取行动。例如,按1表示面积,按2表示体积。如果我没有正确理解要求,请告诉我。考虑到这是一个控制台应用程序,当您作为选项用户时,要求他们输入1或2,并相应地采取行动一个动作。像按1表示面积,按2表示音量。如果我没有正确理解要求,请告诉我。这就是我所想的,但我希望有一个更简单的方法。这就是我所想的,但我希望有一个更简单的方法