c# 需要帮助将 if 语句与 char 数组结合起来

c# need help combining if statement with char array

我是 C# 的绝对初学者,最近尝试创建自己的待办事项列表作为控制台应用程序。 所以我想做的是让用户选择 'a' 'e' 或 'd' 并认为我可以在 if 语句中使用它。

这是我得到的结果:

static void Main(string[] args)
{
    Console.WriteLine("                           To-Do-List");
    Console.ForegroundColor = ConsoleColor.White;
    Console.WriteLine("Task capacity is 10");

    Console.ForegroundColor = ConsoleColor.Red;
    Console.WriteLine("[a] Add Task");
    Console.WriteLine("[e] Edit Task");
    Console.WriteLine("[d] Delete Task");
    Console.ForegroundColor = ConsoleColor.White;

    Console.WriteLine("select action");
    char[] userAction = { 'a', 'e', 'd', };

    if (userAction[0])
    {

    }
}

当我尝试在 if 条件下实现 userAction 变量时,它告诉我它不能转换为布尔值,因为我对语法和所有我不知道的东西不是很自信即使是正确的方法——我是否必须使用其他东西而不是 if 语句?我希望程序做出反应,例如,如果用户按 'a' 和 Console.WriteLine("choose a name for your task");

之类的东西

这一行 userAction[0] 将 return 一个 'a' 的字符值,因为 'a' 是数组中的第一个元素。

您需要在您的 if 语句中添加一个 bool 表达式 (true, false)。

请看下面的例子:

        Console.WriteLine("                           To-Do-List");
        Console.ForegroundColor = ConsoleColor.White;
        Console.WriteLine("Task capacity is 10");

        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine("[a] Add Task");
        Console.WriteLine("[e] Edit Task");
        Console.WriteLine("[d] Delete Task");
        Console.ForegroundColor = ConsoleColor.White;

        Console.WriteLine("select action");
        var inputOption = Console.ReadLine();
        char[] userAction = { 'a', 'e', 'd', };

        if (inputOption == userAction[0].ToString()) //example of if statement
        {
            Console.WriteLine("You choose option to Add task");
        }

        Console.ReadLine();

如果有帮助请告诉我:)

基本上,您正在尝试实现菜单驱动程序,您希望用户通过 select 选项来执行某些操作。您可以尝试以下:

static void Main(string[] args)
{
...
var userAction = 'Y'; //Default assignment to userAction variable
do
{
     Console.WriteLine("[a] Add Task");
     Console.WriteLine("[e] Edit Task");
     Console.WriteLine("[d] Delete Task");
     Console.WriteLine("[y] Exit from Program");
     //Console.Read will give you ability to read action option from an user
     userAction = Console.Read();
     //You can use switch expression as well
     if(Char.ToLower(userAction) == 'a')
     {
         //Perform Add Task action
     }
     else if(Char.ToLower(userAction) == 'e')
     {
         //Perform Edit Task action
     }
     else if(Char.ToLower(userAction) == 'd')
     {
         //Perform Delete Task action
     }
     else if(Char.ToLower(userAction) == 'y')
     {
         Console.WriteLine("Exiting from the program");
     }
     else
     {
         Console.WriteLine("Please enter valid input")
     }
//Do.. while loop will help you to force the user to either press proper options or exit from the program.
}while(Char.ToLower(userAction) == 'y');
}

这里有一些方法可以让您的生活更轻松:

public static string AskString(string question){
  Console.WriteLine(question);
  return Console.ReadLine();
}

public static char AskChar(string question, char[] validChars){
  string input = AskString(question);
  while(input.Length == 0 || (validChars != null && !validChars.Contains(input[0])))
    input = AskString(question);
  
  return input[0];
}

public static int AskInt(string question, int[] validInts){
  string input = AskString(question);
  while(input.Length == 0 || !int.TryParse(input, out int res) || (validInts != null && !validInts.Contains(res)))
    input = AskString(question);
  
  return int.Parse(input);
}

现在您可以在您的程序中使用它们了:

string name = AskString("How old are you?");

int age = AskInt("What age are you?", null);

int choice = AskInt("Choose 1, 2 or 3?", new[]{1,2,3});

char choice2 = AskChar("Choose A, B or C (case sensitive)?", "ABC".ToCharArray());

if(choice == 1 || choice2 = 'B') 
  ...