本文介绍了这必须是数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望如果我写一个字符串就会出错。我怎么能写这个代码只取一个数值。
Hi,
I want that if I write a string that it gives an error. How can I write that this code only take a numeric value.
//This must be a numeric value
Console.Write("Geef je startsalaris in: ");
salaris = Convert.ToDouble(Console.ReadLine());
while(//here should i say that if the user gives a string it will give this error)
{
Console.WriteLine("dit is geen numeriek waarde");
Console.Write("Geef je startsalaris in: ");
salaris = Convert.ToDouble(Console.ReadLine());
}
我的尝试:
i尝试了很多东西,但没有任何解决方案。
What I have tried:
i have tried many things but without any solution.
推荐答案
Console.Write("Geef je startsalaris in: ");
while(!double.TryParse(Console.ReadLine(), out salaris))
{
Console.WriteLine("dit is geen numeriek waarde");
Console.Write("Geef je startsalaris in: ");
}
double x = 0.0;
bool flag = true;
Console.WriteLine("Enter value : ");
while (flag)
{
try
{
x = Convert.ToDouble(Console.ReadLine());
flag = false;
}
catch (Exception ex)
{
Console.WriteLine("Enter correct value :");
flag = true;
}
}
Console.WriteLine("ok :" + x);
这篇关于这必须是数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!