如何检查输入类型并使用 if..else 条件而不会在 c# 中出现错误
How to check a type of an input and use if..else condition without an error in c#
我在做一个必须输入密码的项目。
using System;
namespace program.cs
{
class Program
{
static void Main(string[] args)
{
bool checkpoint1 = false;
long password = 99101L;
Console.WriteLine("Write the Password to Access");
string keyPassword = Console.ReadLine();
if(keyPassword.Length != 0)
{
checkpoint1 = true;
}
else
{
Console.WriteLine("The Password you entered was blank.");
}
if (checkpoint1)
{
long key = Convert.ToInt64(keyPassword);
if (key == password)
{
Console.WriteLine("Access Granted!");
}
else
{
Console.WriteLine("Password Incorrect! \n Access Denied");
}
}
}
}
}
但是当我输入一个字符串作为密码时,它会在 long key = Conver.ToInt64(keyPassword)
行抛出一个错误,它不能更改为 long,所以我该如何解决这个问题,它打印出类型是 a string,如果我尝试 getType 和 typeof() 它,无论 readline 总是接受一个字符串,它都会显示一个字符串。请帮忙。
编辑:我解决了,最终结果代码:
using System;
namespace program.cs
{
class Program
{
static void Main(string[] args)
{
bool checkpoint1 = false;
long password = 99101L;
Console.WriteLine("Write the Password to Access");
string keyPassword = Console.ReadLine();
if(keyPassword.Length != 0)
{
checkpoint1 = true;
}
else
{
Console.WriteLine("The Password you entered was blank.");
}
if (checkpoint1)
{
if(!long.TryParse(keyPassword, out var key))
{
Console.WriteLine("The Password you entered is not a number, please try again");
}
else
{
long Realkey = Convert.ToInt64(keyPassword);
if (Realkey == password)
{
Console.WriteLine("Access Granted!");
}
else
{
Console.WriteLine("Password Incorrect! \n Access Denied");
}
}
}
}
}
}
您可以使用 long.TryParse:
string keyPassword = Console.ReadLine();
if (string.IsNullOrEmpty(keyPassword)) {
Console.WriteLine("The Password you entered was blank.");
}
else if (!long.TryParse(keyPassword, out var key)) {
Console.WriteLine("The Password you entered is not a valid integer.");
}
else if (key != password) {
Console.WriteLine("Password Incorrect! \n Access Denied");
}
else { // Ugh, finally...
Console.WriteLine("Access Granted!");
}
我在做一个必须输入密码的项目。
using System;
namespace program.cs
{
class Program
{
static void Main(string[] args)
{
bool checkpoint1 = false;
long password = 99101L;
Console.WriteLine("Write the Password to Access");
string keyPassword = Console.ReadLine();
if(keyPassword.Length != 0)
{
checkpoint1 = true;
}
else
{
Console.WriteLine("The Password you entered was blank.");
}
if (checkpoint1)
{
long key = Convert.ToInt64(keyPassword);
if (key == password)
{
Console.WriteLine("Access Granted!");
}
else
{
Console.WriteLine("Password Incorrect! \n Access Denied");
}
}
}
}
}
但是当我输入一个字符串作为密码时,它会在 long key = Conver.ToInt64(keyPassword)
行抛出一个错误,它不能更改为 long,所以我该如何解决这个问题,它打印出类型是 a string,如果我尝试 getType 和 typeof() 它,无论 readline 总是接受一个字符串,它都会显示一个字符串。请帮忙。
编辑:我解决了,最终结果代码:
using System;
namespace program.cs
{
class Program
{
static void Main(string[] args)
{
bool checkpoint1 = false;
long password = 99101L;
Console.WriteLine("Write the Password to Access");
string keyPassword = Console.ReadLine();
if(keyPassword.Length != 0)
{
checkpoint1 = true;
}
else
{
Console.WriteLine("The Password you entered was blank.");
}
if (checkpoint1)
{
if(!long.TryParse(keyPassword, out var key))
{
Console.WriteLine("The Password you entered is not a number, please try again");
}
else
{
long Realkey = Convert.ToInt64(keyPassword);
if (Realkey == password)
{
Console.WriteLine("Access Granted!");
}
else
{
Console.WriteLine("Password Incorrect! \n Access Denied");
}
}
}
}
}
}
您可以使用 long.TryParse:
string keyPassword = Console.ReadLine();
if (string.IsNullOrEmpty(keyPassword)) {
Console.WriteLine("The Password you entered was blank.");
}
else if (!long.TryParse(keyPassword, out var key)) {
Console.WriteLine("The Password you entered is not a valid integer.");
}
else if (key != password) {
Console.WriteLine("Password Incorrect! \n Access Denied");
}
else { // Ugh, finally...
Console.WriteLine("Access Granted!");
}