繁体   English   中英

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变量时,它告诉我它不能转换为 boolean 并且因为我对语法并不十分自信,而且我不知道这是否是正确的方法 - 我必须使用其他东西而不是 if 语句? 我希望程序做出反应,例如,如果用户使用Console.WriteLine("choose a name for your task");之类的东西按下“a”。

以下是一些可以让您的生活更轻松的方法:

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') 
  ...

此行 userAction[0] 将返回 'a' 的 char 值,因为 'a' 是数组中的第一个元素。

您需要在 if 语句中添加一个布尔表达式 (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');
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM