我是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‘,程序会做出反应
发布于 2021-04-05 17:02:20
这行userAction将返回字符值'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();如果有帮助,请告诉我:)
发布于 2021-04-05 17:14:06
这里有一些方法可以让你的生活变得更容易:
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')
...发布于 2021-04-05 17:07:25
基本上,您正在尝试实现菜单驱动程序,其中您希望用户选择选项来执行某些操作。您可以在下面进行尝试:
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');
}https://stackoverflow.com/questions/66950456
复制相似问题