C# - 如何处理空的用户输入?
C# - How to handle empty user input?
大家好,总的来说,我还是编程的初学者。所以我的问题是如何在我的简单代码中处理空的用户输入?,每次我在没有值的情况下按 enter 时都会给我错误。
我的代码:
//Program to find the number is even or odd.
using System;
using System.Collection.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace practice
{
class test
{
static void Main (String[] args)
{
int i;
Console.Write (" Enter a number: ");
i = int.parse (Console.ReadLine()); // Where the error occurs when there is no user input.
if(i % 2 ==0)
{
Console.Write (" The number is even");
Console.Read();
}
else
{
Console.Write (" The number is odd");
Console.Read();
}
}
}
}
有什么想法吗?谢谢
试试下面的方法
string line = Console.ReadLine();
if(!string.IsNullOrEmpty(line)){
//Non empty input
}else{
//Handle here
}
您有两个选择:
使用 TryParse
而不是 Parse
。这样就不会抛出异常,您可以测试该值是否有效并再次询问是否有效。
将代码包装在 try
catch
块中以优雅地处理异常并要求用户重试。
在第一种情况下,您最终会得到如下结果:
static void Main (String[] args)
{
int i;
Console.Write (" Enter a number: ");
bool result = int.TryParse(Console.ReadLine(), out i);
if (result)
{
// your normal code
}
else
{
Console.WriteLine("That wasn't a number.");
}
}
在第二种情况下,它会是这样的:
static void Main (String[] args)
{
int i;
try
{
Console.Write (" Enter a number: ");
i = int.parse (Console.ReadLine());
}
catch (Exception)
{
Console.WriteLine("That wasn't a number.");
return;
}
// rest of your code
}
您可以使用 String.IsNullOrWhiteSpace 方法检查用户输入是否为空。此方法检查任何输入的空格或空输入。
您可以这样检查您的输入:
if (string.IsNullOrWhiteSpace(line))
{
...//parse
}
然而,由于您正在解析整数,因此您可能希望使用 Int32.TryParse 函数来解析数据。它将 return 一个布尔值,指示输入是否已成功解析。
int i =0;
if (Int32.TryParse(Console.ReadLine(), out i))
{
...//continue
}
static void Main(string[] args)
{
int i;
Console.Write(" Enter a number: ");
if (Int32.TryParse(Console.ReadLine(), out i))
{
if (i % 2 == 0)
{
Console.Write(" The number is even");
}
else
{
Console.Write(" The number is odd");
}
}
else
{
Console.Write(" You have to enter a number I can parse into an integer, dummy!");
}
}
大家好,总的来说,我还是编程的初学者。所以我的问题是如何在我的简单代码中处理空的用户输入?,每次我在没有值的情况下按 enter 时都会给我错误。 我的代码:
//Program to find the number is even or odd.
using System;
using System.Collection.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace practice
{
class test
{
static void Main (String[] args)
{
int i;
Console.Write (" Enter a number: ");
i = int.parse (Console.ReadLine()); // Where the error occurs when there is no user input.
if(i % 2 ==0)
{
Console.Write (" The number is even");
Console.Read();
}
else
{
Console.Write (" The number is odd");
Console.Read();
}
}
}
}
有什么想法吗?谢谢
试试下面的方法
string line = Console.ReadLine();
if(!string.IsNullOrEmpty(line)){
//Non empty input
}else{
//Handle here
}
您有两个选择:
使用
TryParse
而不是Parse
。这样就不会抛出异常,您可以测试该值是否有效并再次询问是否有效。将代码包装在
try
catch
块中以优雅地处理异常并要求用户重试。
在第一种情况下,您最终会得到如下结果:
static void Main (String[] args)
{
int i;
Console.Write (" Enter a number: ");
bool result = int.TryParse(Console.ReadLine(), out i);
if (result)
{
// your normal code
}
else
{
Console.WriteLine("That wasn't a number.");
}
}
在第二种情况下,它会是这样的:
static void Main (String[] args)
{
int i;
try
{
Console.Write (" Enter a number: ");
i = int.parse (Console.ReadLine());
}
catch (Exception)
{
Console.WriteLine("That wasn't a number.");
return;
}
// rest of your code
}
您可以使用 String.IsNullOrWhiteSpace 方法检查用户输入是否为空。此方法检查任何输入的空格或空输入。
您可以这样检查您的输入:
if (string.IsNullOrWhiteSpace(line))
{
...//parse
}
然而,由于您正在解析整数,因此您可能希望使用 Int32.TryParse 函数来解析数据。它将 return 一个布尔值,指示输入是否已成功解析。
int i =0;
if (Int32.TryParse(Console.ReadLine(), out i))
{
...//continue
}
static void Main(string[] args)
{
int i;
Console.Write(" Enter a number: ");
if (Int32.TryParse(Console.ReadLine(), out i))
{
if (i % 2 == 0)
{
Console.Write(" The number is even");
}
else
{
Console.Write(" The number is odd");
}
}
else
{
Console.Write(" You have to enter a number I can parse into an integer, dummy!");
}
}