用户输入字符而不是双精度时的错误处理
Error-handling when user inputs character instead of double
我做了一个简单的应用程序,根据本金年限和利率计算贷款的每月还款额。如果用户输入一个字符而不是双字符,程序就会崩溃。当有人插入信件时,如何创建一条消息说 "please input valid number"?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW1Udeshi
{
class Program
{
static void Main(string[] args)
{
bool Continue;
while (true)
{
//initialize variables
double r, yrs, m, p;
string input, output;
//principle amount
Console.Write("Enter a Principle Amount: ");
input = Console.ReadLine();
//Int32.Parse
p = double.Parse(input);
//Number of years
Console.Write("Enter Number of Years: ");
input = Console.ReadLine();
//Int32.Parse
yrs = double.Parse(input);
//Interest Rate
Console.Write("Enter Interest rate: ");
input = Console.ReadLine();
//Int32.Parse
r = double.Parse(input);
//monthly payment formula:
m = (p * r / 1200.0) / (1 - Math.Pow((1.0 + r / 1200.0),(-12 * yrs)));
//Round two decimal pts
m = Math.Round(m,2);
//output
output = m.ToString();
Console.WriteLine("Monthly payment is " + m + ".");
Console.ReadLine();
Console.WriteLine("Do you want to Continue y/n?");
if (Console.ReadLine() == "y")
{
Continue = true;
}
else
{
Environment.Exit(0);
}
}
}
}
}
使用 TryParse
而不是 int.parse 这样的东西
Console.Write("Enter a Principle Amount: ");
input = Console.ReadLine();
double.TryParse(input,out p);
if(p== 0)
{
Console.Write("please input valid number");
}
我通常为它写一个单独的方法,使用double.TryParse
因为它return如果转换成功则为真并将转换后的值分配给out参数。在下面的示例中,如果 TryParse
成功,我将跳出 while (true)
循环并获得 return 结果。否则,我告诉用户再试一次:
private static double GetDoubleFromUser(string prompt)
{
double result;
while (true)
{
if (prompt != null) Console.Write(prompt);
var input = Console.ReadLine();
if (double.TryParse(input, out result)) break;
Console.WriteLine("Sorry, that is not a valid number. Please try again...");
}
return result;
}
然后,在你的主代码中,你不必担心输入和输出或验证,你可以做这样的事情:
double principal = GetDoubleFromUser("Enter a Principal Amount: ");
double years = GetDoubleFromUser("Enter Number of Years: ");
double rate = GetDoubleFromUser("Enter Interest rate: ");
我做了一个简单的应用程序,根据本金年限和利率计算贷款的每月还款额。如果用户输入一个字符而不是双字符,程序就会崩溃。当有人插入信件时,如何创建一条消息说 "please input valid number"?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW1Udeshi
{
class Program
{
static void Main(string[] args)
{
bool Continue;
while (true)
{
//initialize variables
double r, yrs, m, p;
string input, output;
//principle amount
Console.Write("Enter a Principle Amount: ");
input = Console.ReadLine();
//Int32.Parse
p = double.Parse(input);
//Number of years
Console.Write("Enter Number of Years: ");
input = Console.ReadLine();
//Int32.Parse
yrs = double.Parse(input);
//Interest Rate
Console.Write("Enter Interest rate: ");
input = Console.ReadLine();
//Int32.Parse
r = double.Parse(input);
//monthly payment formula:
m = (p * r / 1200.0) / (1 - Math.Pow((1.0 + r / 1200.0),(-12 * yrs)));
//Round two decimal pts
m = Math.Round(m,2);
//output
output = m.ToString();
Console.WriteLine("Monthly payment is " + m + ".");
Console.ReadLine();
Console.WriteLine("Do you want to Continue y/n?");
if (Console.ReadLine() == "y")
{
Continue = true;
}
else
{
Environment.Exit(0);
}
}
}
}
}
使用 TryParse
而不是 int.parse 这样的东西
Console.Write("Enter a Principle Amount: ");
input = Console.ReadLine();
double.TryParse(input,out p);
if(p== 0)
{
Console.Write("please input valid number");
}
我通常为它写一个单独的方法,使用double.TryParse
因为它return如果转换成功则为真并将转换后的值分配给out参数。在下面的示例中,如果 TryParse
成功,我将跳出 while (true)
循环并获得 return 结果。否则,我告诉用户再试一次:
private static double GetDoubleFromUser(string prompt)
{
double result;
while (true)
{
if (prompt != null) Console.Write(prompt);
var input = Console.ReadLine();
if (double.TryParse(input, out result)) break;
Console.WriteLine("Sorry, that is not a valid number. Please try again...");
}
return result;
}
然后,在你的主代码中,你不必担心输入和输出或验证,你可以做这样的事情:
double principal = GetDoubleFromUser("Enter a Principal Amount: ");
double years = GetDoubleFromUser("Enter Number of Years: ");
double rate = GetDoubleFromUser("Enter Interest rate: ");