如何循环Console.ReadLine
本文关键字:Console ReadLine 循环 何循环 | 更新日期: 2023-09-27 18:10:59
我不知道如何在循环中读取用户输入(使用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);
}
一般的格式是这样的(while循环和break条件):
// put code above while loop that only needs to be executed once
while (true) {
// get the user input for every iteration, allowing to exit at will
String line = Console.ReadLine();
if (line.Equals("exit")) {
// exit the method.
return; // use "break" if you just want to exit the loop
}
// this is what will happen in the loop body since we didn't exit
// put whatever note stuff you want to execute again and again in here
}
您将需要编辑进入该循环主体的内容,这取决于您想要对注释实例做什么。但一般来说,您会反复提示用户输入,直到满足某个条件,然后跳出循环。你可以决定那个条件(例如:"输入10个音符";"退出型";等)
根据@n0rd的评论,这是如何做的…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));
要循环Console.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<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().ToLower()) != exitKey)
{ //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
}
:
while ((callSign = Console.ReadLine().ToLower()) != exitKey) {
...
是循环发生的地方。如果条目不等于exitKey,则重复上述步骤。