如何阻止程序在C#中进一步执行 [英] How to stop program executing further in C#

查看:107
本文介绍了如何阻止程序在C#中进一步执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string FirstName = Console.ReadLine();
            if (FirstName.Length > 12)
            {
                Console.WriteLine(".......................................");
            }
            if(FirstName.Length<3)
            {
                Console.WriteLine("....................");
            }
            Console.WriteLine("...................");
            string SecondName = Console.ReadLine();
            if (SecondName.Length > 12)
            {
                Console.WriteLine(".............................");
            }
            if(SecondName.Length<3)
            {

我想,如果他们按没有把一个值进入停止程序,该怎么办呢?/?

I want to stop the program if they press enter without putting a value,how to do it??/?

推荐答案

我想你想有一个非空字符串值从控制台输入,如果输入为空,则希望终止应用程序。使用下面的代码:

I think you want to have a non empty string value as an input from console and if input is empty then want to terminate your application. Use following code:

Console.WriteLine("Enter a value: ");
string str = Console.ReadLine();
//If pressed enter here without a  value or data, how to stop the  program here without
//further execution??
if (string.IsNullOrWhiteSpace(str))
  return;
else
{
  Console.WriteLine(string.Format("you have entered: '{0}'", str));
  Console.Read();
}

如果用户输入任何类型的空字符串或空格的,应用程序将被终止的那一刻,他/她会按enter键。

If user will enter any kind of empty string or white spaces, the application will be terminated the moment he/she will press enter.

这篇关于如何阻止程序在C#中进一步执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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