Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在arrayc#中键入字符串时,是否有方法引发异常?_C#_Arrays_Exception_Throw - Fatal编程技术网

在arrayc#中键入字符串时,是否有方法引发异常?

在arrayc#中键入字符串时,是否有方法引发异常?,c#,arrays,exception,throw,C#,Arrays,Exception,Throw,我是这个迷人的编程世界的新手。我已经完成了这个数组,但是当我键入一个非整数时,它崩溃了。我尝试过很多方法,比如int.Parse(console.readLine))、tryparse(text,out int)和ConvertTo32,但是它仍然说“输入字符串的格式不正确。”谢谢 使用制度 名称空间气泡排序 { 班级计划 { publicstaticvoidhelpme(int[]a,int-t) { 对于(int j=0;j您应该使用int.TryParse方法验证用户取消打印。如果输入的字

我是这个迷人的编程世界的新手。我已经完成了这个数组,但是当我键入一个非整数时,它崩溃了。我尝试过很多方法,比如int.Parse(console.readLine))、tryparse(text,out int)和ConvertTo32,但是它仍然说“输入字符串的格式不正确。”谢谢

使用制度

名称空间气泡排序 { 班级计划 {

publicstaticvoidhelpme(int[]a,int-t)
{

对于(int j=0;j您应该使用int.TryParse方法验证用户取消打印。如果输入的字符串可以转换为int,则只应将其插入数组,否则程序应该忽略该值

static void Main(string[] args)
{
    int[] num = { 1, 2, 3, 4, 5 };
    int[] a = new int[5];


    for (int x = 0; x < 5; x++)
    {
        Console.WriteLine($"Input enter {num[0 + x]} of five");
        int temp = 0;
        
        string input = Console.ReadLine();
        
        if(int.TryParse(input, out temp))
        {
          a[0 + x] = Convert.ToInt32(input);
        }
                                
    }

    Console.WriteLine("The Array is : ");

    for (int i = 0; i < a.Length; i++)
    {
        Console.WriteLine(a[i]);
    }


    {

        HelpME(num, 5);

    }

    Console.WriteLine("The Sorted Array :");

    foreach (int aray in a)
    {
        Console.Write(aray + " ");
    }
    Console.ReadLine();
}
static void Main(字符串[]args)
{
int[]num={1,2,3,4,5};
int[]a=新的int[5];
对于(int x=0;x<5;x++)
{
WriteLine($“输入输入{num[0+x]},共五个”);
内部温度=0;
字符串输入=Console.ReadLine();
if(输入、输出温度)
{
a[0+x]=转换为32(输入);
}
}
WriteLine(“数组是:”);
for(int i=0;i
为什么要抛出异常?
Convert.ToInt32
已经这样做了。您可以捕获该异常,也可以使用,它返回一个布尔值,告诉您是否可以解析字符串。(我建议阅读我链接的手册以了解如何使用它)我想通过验证是否只输入了数字来保护程序。我正在尝试得到的结果应该类似于“Input number 2 of five”jhljkhlk“如果没有输入错误的值,请重试”感谢您的解决方案是捕获异常。或者使用
TryParse
。我仍在尝试获得类似这样的结果,例如“输入数字2/5”,然后我键入一个类似dsbkhjdsbk的非整数,我希望获得此输出“未输入错误的值,请重试”我通过这样做解决了它:if(int.TryParse(输入,输出温度){a[0+x]=Convert.ToInt32(输入);}else Console.WriteLine(“未输入正确的值,请重试”,a);在问题中,您没有要求您还需要一条输出消息用于错误的输入,这就是为什么我没有在代码中插入else部分
static void Main(string[] args)
{
    int[] num = { 1, 2, 3, 4, 5 };
    int[] a = new int[5];


    for (int x = 0; x < 5; x++)
    {
        Console.WriteLine($"Input enter {num[0 + x]} of five");
        int temp = 0;
        
        string input = Console.ReadLine();
        
        if(int.TryParse(input, out temp))
        {
          a[0 + x] = Convert.ToInt32(input);
        }
                                
    }

    Console.WriteLine("The Array is : ");

    for (int i = 0; i < a.Length; i++)
    {
        Console.WriteLine(a[i]);
    }


    {

        HelpME(num, 5);

    }

    Console.WriteLine("The Sorted Array :");

    foreach (int aray in a)
    {
        Console.Write(aray + " ");
    }
    Console.ReadLine();
}