我如何使用tryparse? [英] How do I use tryparse ?

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

问题描述

我希望用户必须填写邮政编码。邮政编码包含4位数字,如果邮政编码包含更多或更少的数字,则代码将显示错误

I want that the user must fill in a zip code.The Zip code contains 4 digits if the zip code has more or less digits then the code will indicate an error

    Console.Write("Geef je postcode: ");
    postcode =  Console.ReadLine();


while (int.TryParse(postcode, out))// i want that if the zip code has more or less than 4 digits then it will give the code in the while.
{

    Console.WriteLine("ongeldige postcode");
    Console.WriteLine("Geef je postcode: ");
    postcode = Console.ReadLine();
}





我尝试过:





What I have tried:

while (int.TryParse(postcode, out))



但我不知道如何使用tryparse。


but i don't know how to use tryparse.

推荐答案

Tryparse尝试从一种类型转换到另一种类型,它不测试长度。



邮政编码是一个字符串,所以使用.Length来测试<> 4.



Tryparse attempts a conversion from one type to another, it does not test the length.

postcode is a string so use the .Length to test <> 4.

while (postcode.Length != 4)// i want that if the zip code has more or less than 4 digits then it will give the code in the while.
{
 
    Console.WriteLine("ongeldige postcode");
    Console.WriteLine("Geef je postcode: ");
    postcode = Console.ReadLine();
}


这篇关于我如何使用tryparse?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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