小编Hag*_*ill的帖子

C#如何使用循环外部的变量在循环内从string更改为int?

代码:

string[] numbers = new string[2];
for (int i = 0; i < numbers.Length; i++)
{
    numbers[i] = Console.ReadLine();
    if (int.TryParse(numbers[i], out int numberTry) && i == 0)
        Console.WriteLine("That would be a number yes.");

    else if (int.TryParse(numbers[i], out numberTry))
        Console.WriteLine("Lovely work! That is indeed two numbers!");

    else
    {
        Console.Clear();
        Console.WriteLine("That's not a number. I am dissapointed.");
        Console.ReadKey();
        Environment.Exit(0);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的问题在于我不能将循环外的数字[]用作int,因为它从循环中的字符串转换为int.我需要能够使用方法作为int发送它来对数字[0]和数字[1]执行操作.提前致谢!

c# for-loop tryparse

3
推荐指数
1
解决办法
403
查看次数

标签 统计

c# ×1

for-loop ×1

tryparse ×1