如何循环一个Console.ReadLine? [英] How to loop a Console.ReadLine?

查看:54
本文介绍了如何循环一个Console.ReadLine?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚如何在循环中读取用户输入(使用 Console.ReadLine ).我正在尝试创建一个便条,该便条可以存储用户输入的内容,如果用户输入exit则退出.

I cannot figure out how to read user-input in a loop (with Console.ReadLine). I'm trying to create a note that lets me store what ever the user inputs, and exits if he types 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 输入检查用户是否确实要求退出,否则请继续添加注释.

You need List of Notes in order to add as many notes as you want. Additionally, you need to first save ReadLine input check if the user really asked to exit otherwise keep adding notes.

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);
}

这篇关于如何循环一个Console.ReadLine?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆