C#cosole app console readline错误 [英] C# cosole app console readline error

查看:120
本文介绍了C#cosole app console readline错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在console.readline中读取整数输入我的数据类型是int ..或者如何在console.readline中取字符串和int ..

看到我的邮政编码它显示错误当我输入



我尝试过:



how to read input as integer in console.readline my data type is int .. or how to take string and int in console.readline..
seee my postal code it show error when i input

What I have tried:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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;

            Console.WriteLine("enter your first name");
            firstname = Console.ReadLine();
            Console.WriteLine("enter last name");
           lastname = Console.ReadLine();
            Console.WriteLine("brithdate");
            birthdate =Console.ReadLine();
            Console.WriteLine("address line 1");

           addressline1 = Console.ReadLine();
            Console.WriteLine("addres line 2");
            adressline2= Console.ReadLine();
            Console.WriteLine("city");
            city = Console.ReadLine();
            Console.WriteLine("state/province");
            stateorprovince = Console.ReadLine();
            Console.WriteLine("zip or postal code");
            ziporpostalcode =int.Parse(Console.ReadLine());
            Console.WriteLine("country");
            country = Console.ReadLine();
        }
    }
}

推荐答案

试试这个



try this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {


        static void Main(string[] args)
        { 
            int ziporpostalcode;            
            Console.WriteLine("zip or postal code");
            ziporpostalcode = GetZipCode();
          
        }

        private static int GetZipCode()
        {
            int ziporpostalcode;
            here:
            string zip = Console.ReadLine();
            if (!int.TryParse(zip, out ziporpostalcode))
            {
                Console.WriteLine("Please enter a valid zip code");
                goto here;
            }
            return ziporpostalcode;
        }
    }
}


您好,





尝试下面的代码。



Hi,


Try below code.

ziporpostalcode =Convert.ToInt32(Console.ReadLine());





谢谢,

Raghuveer



Thanks,
Raghuveer


使用TryParse,如果你将输入int然后它将按原样转换,但如果你将在邮政编码中输入字符串,它将给你0.



use TryParse, if you will enter int then it will convert as it is, but if you will enter string in zip code it will give you 0.

static void Main(string[] args)
       {

           string firstname;
           string lastname;
           string birthdate;
           string addressline1;
           string adressline2;
           string city;
           string stateorprovince;
           int ziporpostalcode;
           string country;

           Console.WriteLine("enter your first name");
           firstname = Console.ReadLine();
           Console.WriteLine("enter last name");
           lastname = Console.ReadLine();
           Console.WriteLine("brithdate");
           birthdate = Console.ReadLine();
           Console.WriteLine("address line 1");

           addressline1 = Console.ReadLine();
           Console.WriteLine("addres line 2");
           adressline2 = Console.ReadLine();
           Console.WriteLine("city");
           city = Console.ReadLine();
           Console.WriteLine("state/province");
           stateorprovince = Console.ReadLine();
           Console.WriteLine("zip or postal code");
            Int32.TryParse(Console.ReadLine(), out ziporpostalcode);
           Console.WriteLine("country");
           country = Console.ReadLine();

       }


这篇关于C#cosole app console readline错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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