Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 控制台应用程序输入和输出_C#_Asp.net_Linq - Fatal编程技术网

C# 控制台应用程序输入和输出

C# 控制台应用程序输入和输出,c#,asp.net,linq,C#,Asp.net,Linq,这是我的简单C#控制台应用程序,在这里,我将从用户那里获得输入。我有一个邮政编码变量,我想将输入作为整数,但当我输入整数时,它显示错误。另一种方法是console.readline是否同时使用int和string作为输入 namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string firstname; string las

这是我的简单C#控制台应用程序,在这里,我将从用户那里获得输入。我有一个邮政编码变量,我想将输入作为整数,但当我输入整数时,它显示错误。另一种方法是console.readline是否同时使用int和string作为输入

namespace ConsoleApplication1
{
  class Program
  {

    static void Main(string[] args)
    {
        string firstname;
        string lastname;
        string birthdate;
        string addressline1;
        string adressline2;
        string city;
        string stateorprovince;
        int    ziporpostalcode;
        string country;        
        ziporpostalcode =int.Parse(Console.ReadLine());          
    }
}
}
您应该使用for
int.Parse
,这是 负责将数字的字符串表示形式转换为其 等效32位有符号整数。返回值指示 操作成功,否则返回false(转换失败)

因此,您的代码可能如下所示:

int ziporpostalcode;
if (int.TryParse(Console.ReadLine(), out ziporpostalcode))
{
    Console.WriteLine("Thank you for entering Correct ZipCode");
    // now ziporpostalcode will contains the required value
   // Proceed with the value
}
else {
    Console.WriteLine("invalid zipCode");
}
Console.ReadKey(); 
您应该使用for
int.Parse
,这是 负责将数字的字符串表示形式转换为其 等效32位有符号整数。返回值指示 操作成功,否则返回false(转换失败)

因此,您的代码可能如下所示:

int ziporpostalcode;
if (int.TryParse(Console.ReadLine(), out ziporpostalcode))
{
    Console.WriteLine("Thank you for entering Correct ZipCode");
    // now ziporpostalcode will contains the required value
   // Proceed with the value
}
else {
    Console.WriteLine("invalid zipCode");
}
Console.ReadKey(); 
建议的方法

用于验证对
int
的输入

var input =int.Parse(Console.ReadLine());    
if(int.TryParse(input, out ziporpostalcode )
{
    // you have int zipcode here
}
else
{
   // show error. 
}
建议的方法

用于验证对
int
的输入

var input =int.Parse(Console.ReadLine());    
if(int.TryParse(input, out ziporpostalcode )
{
    // you have int zipcode here
}
else
{
   // show error. 
}

啊。。。是的,谢谢@un lucky。现在修好了。啊。。。是的,谢谢@un lucky。现在已修复。只是一个友好的提示,您可能希望阅读本页:因此您可以始终确保您的问题易于回答且尽可能清楚。请确保包括您为解决问题所做的任何努力,以及在尝试这些修复时发生的情况。另外,不要忘记显示代码和任何错误消息!如果我们只知道“它显示错误”,那么我们无法帮助您修复错误。这意味着什么?只是一个友好的提示,你可能想阅读这一页:这样你就可以始终确保你的问题很容易回答,并且尽可能清楚。请确保包括您为解决问题所做的任何努力,以及在尝试这些修复时发生的情况。另外,不要忘记显示代码和任何错误消息!如果我们只知道“它显示错误”,那么我们无法帮助您修复错误。这意味着什么?如何使不使用if-else条件或例外变得容易如何使不使用if-else条件或例外变得容易