我不知道如何在循环中读取用户输入(使用 Console.ReadLine
)。我正在尝试创建一个注释,让我可以存储用户输入的内容,并在他键入 exit 时退出。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
Note myNote = new Note();
Note otherNote = new Note();
myNote.addText("Hi there");
Console.WriteLine(myNote.display());
otherNote.addText(Console.ReadLine());
Console.WriteLine(otherNote.display());
if (otherNote = "exit")
{
}
}
}
}
class Note
{
private string text = "";
private DateTime timeStamp = DateTime.Now;
private DateTime modifiedStamp = DateTime.Now;
int maxLength = 10;
public void addText(string sometext)
{
if (text.Length + sometext.Length < maxLength)
{
text += sometext;
modifiedStamp = DateTime.Now;
}
}
public string display()
{
return "Created: " + timeStamp.ToString() + "\n" +
"Modified: " + modifiedStamp.ToString() + "\n" +
"Content: " + text;
}
}
最佳答案
您需要笔记列表才能添加任意数量的笔记。
此外,您需要先保存 ReadLine
输入检查用户是否真的要求退出,否则继续添加注释。
var myNotes = new List<Note>();
var firstNote = new Note();
firstNote.addText("Hi there");
Note note;
while (true)
{
var input = Console.ReadLine();
if (input.Equals("exit", StringComparison.OrdinalIgnoreCase))
{
break;
}
note = new Note();
note.addText(input);
myNotes.Add(note);
}
关于c# - 如何循环 Console.ReadLine?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32363719/