我无法弄清楚如何在循环中读取用户输入(使用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;
}
}
答案 0 :(得分:7)
您需要注释列表才能添加任意数量的注释。
此外,您需要首先保存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);
}
答案 1 :(得分:0)
一般格式是使用类似这样的东西(带有中断条件的while循环):
// 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
}
根据您对音符实例的确切要求,您将要编辑此循环体内的内容。但通常情况下,您会反复提示用户输入,直到满足某些条件,然后您就会退出循环。您可以决定条件(例如&#34;输入10个音符&#34 ;;&#34;输入退出&#34;等等)
答案 2 :(得分:0)
Per @ 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));
答案 3 :(得分:0)
这样做的一种方法是:
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
}
该行:
while ((callSign = Console.ReadLine()) != exitKey.ToLower()) {
...
是循环发生的地方。如果条目不等于exitKey,则重复这些步骤。
答案 4 :(得分:0)
要循环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
}`