我是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语句吗?我希望程序做出反应,例如,如果用户用类似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",因为"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();
如果有帮助,请告诉我:(
基本上,您正在尝试实现菜单驱动的程序,您希望用户选择选项来执行某些操作。您可以尝试以下内容:
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');
}