【发布时间】:2021-12-02 11:31:33
【问题描述】:
我正在尝试在 C# 中创建一个 todo 应用程序。我想要做的是能够以某种方式将任务标记为已完成。有谁知道怎么做?
这是我的程序类,我在 foreach 中打印出列表:
using System;
namespace ToDo
{
class Program
{
static void Main(string[] args)
{
bool isRunning = true;
var collection = new TodoCollection();
while (isRunning)
{
var menu = new menu();
menu.Title();
menu.Options();
int choices;
choices = Convert.ToInt32(Console.ReadLine());
switch (choices)
{
case 1:
var inputNewTask = Console.ReadLine();
var task = new Task(inputNewTask, false);
collection.Add(task);
Console.Clear();
Console.ReadLine();
break;
case 2:
int counter = 0;
if (collection != null)
{
Console.WriteLine("Dagens uppgifter listas här nedanför");
foreach (Task newTask in collection)
{
counter++;
Console.WriteLine($"({counter}) [] {newTask.ShowTask()}");
}
int userInput = Convert.ToInt32(Console.ReadLine());
Task selectedTask = collection.GetTask(userInput);
selectedTask.Done();
}
break;
}
}
}
}
}
在我的 foreach 循环中有一个空方括号,我想通过用户输入将其更改为“X”。
这是我的 Todo 集合的类,其中包含我的列表:
using System.Collections;
using System.Collections.Generic;
namespace ToDo
{
public class TodoCollection : IEnumerable<Task>
{
//Deklarerar mina fält
List<Task> _CurrentTasks = new List<Task>();
//Funktion som låter användaren läga till nya tasks i listan
public void Add(Task NewTask)
{
_CurrentTasks.Add(NewTask);
}
public Task GetTask(int userInput)
{
return _CurrentTasks[userInput];
}
//Låter mig iterera igenomm listan _CurrentTasks
public IEnumerator<Task> GetEnumerator()
{
return ((IEnumerable<Task>)_CurrentTasks).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable)_CurrentTasks).GetEnumerator();
}
}
}
``´
我也在这里为我的任务开设了一门课:
namespace ToDo
{
public class Task
{
string _Name;
bool isDone = false;
public Task(string name, bool done)
{
this._Name = name;
this.isDone = done;
}
public string ShowTask()
{
return this._Name;
}
public bool Done()
{
isDone = true;
return isDone;
}
}
}
【问题讨论】:
-
您当前的程序不起作用怎么办?您已经阅读了用户输入并对其进行处理以将任务设置为完成。请注意,控制台应用程序不会产生对用户最友好的待办事项应用程序...
-
是的,我知道。但是我想这样做,如果用户在当前待办事项显示时按下例如“1”,我希望我的 consoole.WriteLine 中的括号用“X”标记
-
如果您的意思是一种“交互”方式,控制台应用程序通常不适合这种用户交互。你可以模拟这个,见:stackoverflow.com/questions/60767909/…