c# – 如何循环Console.ReadLine?
发布时间:2020-12-15 23:27:08 所属栏目:百科 来源:网络整理
导读:我无法弄清楚如何在循环中读取用户输入(使用Console.ReadLine).我正在尝试创建一个注释,让我存储用户输入的内容,如果他输入退出则退出. using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;n
我无法弄清楚如何在循环中读取用户输入(使用Console.ReadLine).我正在尝试创建一个注释,让我存储用户输入的内容,如果他输入退出则退出.
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); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |