获取控制台应用程序以允许多次输入 [英] Getting a console application to allow input multiple times

查看:131
本文介绍了获取控制台应用程序以允许多次输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个控制台应用程序,该应用程序计算自用户指定日期以来的天数。但是,在原始计算之后,如果输入了另一个日期,则应用程序将关闭。

I made a console application that calculates the number of days since a user-specified date. But after the original calculation, if another date is typed in, the application closes.

是否存在一种方法,如果用户希望继续,则可以使我的应用程序不关闭

Is there a way I can get my application to not close if the user wants to continue using it?

Console.WriteLine("Please enter the date you wish to specify: (DD/MM/YYYY)");
        string userInput;
        userInput = Console.ReadLine();
        DateTime calc = DateTime.Parse(userInput);
        TimeSpan days = DateTime.Now.Subtract(calc);
        Console.WriteLine(days.TotalDays);
        Console.ReadLine();


推荐答案

执行while循环:

Console.WriteLine("Please enter the date you wish to specify: (DD/MM/YYYY)");
string userInput;
userInput = Console.ReadLine();
while (userInput != "0")
{
    DateTime calc = DateTime.Parse(userInput);
    TimeSpan days = DateTime.Now.Subtract(calc);
    Console.WriteLine(days.TotalDays);
    Console.WriteLine("Add another date");
    userInput = Console.ReadLine();
}

按0并输入将退出。

这篇关于获取控制台应用程序以允许多次输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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