C# 如何循环Console.ReadLine?
我不知道如何在循环中读取用户输入(使用C# 如何循环Console.ReadLine?,c#,C#,我不知道如何在循环中读取用户输入(使用Console.ReadLine)。我试图创建一个便笺,让我存储用户输入的内容,如果用户键入exit,我将退出 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication4 { class Program {
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;
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间控制台应用程序4
{
班级计划
{
静态void Main(字符串[]参数)
{
Note myNote=新注释();
Note otherNote=新注释();
myNote.addText(“你好”);
Console.WriteLine(myNote.display());
otherNote.addText(Console.ReadLine());
Console.WriteLine(otherNote.display());
如果(otherNote=“退出”)
{
}
}
}
}
课堂笔记
{
私有字符串text=“”;
private DateTime timeStamp=DateTime.Now;
private DateTime modifiedStamp=DateTime.Now;
int maxLength=10;
公共void addText(字符串sometext)
{
if(text.Length+sometext.Length
您需要一个注释列表,以便添加任意数量的注释。
此外,如果用户确实要求退出,则需要首先保存ReadLine
input检查,否则继续添加注释
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);
}
var myNotes=newlist();
var firstNote=新注释();
firstNote.addText(“你好”);
注释;
while(true)
{
var input=Console.ReadLine();
if(input.Equals(“exit”,StringComparison.OrdinalIgnoreCase))
{
打破
}
注释=新注释();
注:添加文本(输入);
添加(注释);
}
一般的格式是使用这样的内容(带有中断条件的while循环):
您将需要编辑进入此循环主体的内容,具体取决于您希望对Notes实例执行的操作。但通常情况下,您会反复提示用户输入,直到满足某些条件,然后才中断循环。您可以决定该条件(例如“输入10个注释”、“键入退出”等)根据@n0rd的注释,do…while循环的工作方式如下:
string input;
var myNotes = new List<Note>();
do{
input = Console.ReadLine();
if (!input.Equals("exit", StringComparison.OrdinalIgnoreCase)){
var note = new Note();
note.addText(input);
myNotes.Add(note);
}
} while (!input.Equals("exit", StringComparison.OrdinalIgnoreCase));
字符串输入;
var myNotes=新列表();
做{
input=Console.ReadLine();
如果(!input.Equals(“exit”,StringComparison.OrdinalIgnoreCase)){
var note=新注释();
注:添加文本(输入);
添加(注释);
}
}而(!input.Equals(“exit”,StringComparison.OrdinalIgnoreCase));
一种方法是:
List<string> simpleList = new List<string> { "Alpha", "Bravo", "Charlie", "Delta", "Echo" }; //Dummy data source
Console.WriteLine("Enter a call sign to find in the list. Press X to exit: "); //Prompt
string callSign;
string exitKey = "x";
while ((callSign = Console.ReadLine()) != exitKey.ToLower()) { //This is where the "Magic" happens
if (simpleList.Contains(callSign)) {
Console.WriteLine($"\"{callSign}\" exists in our simple list");//Output should the list contain our entry
Console.WriteLine(""); //Not really relevant, just needed to added spacing between input and output
}
else {
Console.WriteLine($"\"{callSign}\" does not exist in our simple list"); //Output should the list not contain our entry
}
Console.WriteLine("");
Console.WriteLine("Enter a call sign to find in the list. Press X to exit: ");//Prompt
}
这就是循环发生的地方。如果条目不等于exitKey,则重复这些步骤。要循环控制台。ReadLine()您可以使用它
`List<string> al = new List<string>(); //list to store string values
while(true)
{
string f = Console.ReadLine();
if(f == null) //check if string is null or not
{
break;
}
else
al.Add(f); //add strings to list
}`
`List al=new List()//用于存储字符串值的列表
while(true)
{
字符串f=Console.ReadLine();
if(f==null)//检查字符串是否为null
{
打破
}
其他的
al.Add(f);//将字符串添加到列表中
}`
发布前将代码粘贴到VS。它将格式化我编辑的代码..只是为了澄清一下,你想循环writeline,这样用户就可以输入注释,直到它退出?因此,用户可以输入0个注释、1个注释、2个注释、99个注释或n个注释?与?很抱歉,新加入此网站。请使用带中断条件的while循环。@edM VS=Visual Studio。我认为他希望添加N个注释,直到用户输入“退出”。这两个新音符是个错误。我可以添加一个支持注释列表的答案,但我认为这对我来说有点粗鲁。do。。。而
循环在这里更合适。并允许合理的退出条件,而不仅仅是而(true)
好的,谢谢大家。我想我明白你们的意思了,(我想…)我对C很陌生,非常感谢你们的回复@eser代码直接从visual Studio复制studio@n0rd关于do的好观点。。。虽然
对于控制台菜单更好,但我不能将退出添加为文本注释:我尝试了你的代码,它可以工作,但“bool kepAddingNotes=true;”在VS中给我警告,表示赋值,但从未使用过,。。顺便说一句,谢谢@Orel Eraki如何在您的代码上应用我的方法显示只需添加:Console.WriteLine(note.display())代码>之后myNotes.Add(注释)代码>
while ((callSign = Console.ReadLine()) != exitKey.ToLower()) {
...
`List<string> al = new List<string>(); //list to store string values
while(true)
{
string f = Console.ReadLine();
if(f == null) //check if string is null or not
{
break;
}
else
al.Add(f); //add strings to list
}`