Доступ к переменной, объявленной в теле цикла - C#
Формулировка задачи:
Здравствуйте, есть следующий код
проблема в том, что не смотря на то что все переменные объявлены вне цикла, переменная last остается пустой, то есть ее значение за цикл for не передается, что делать? Подскажите пожалуйста!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace lab_5_18
{
public class check
{
public int chek_values()
{
bool access;
int n;
while (true)
{
string s = Console.ReadLine();
access = int.TryParse(s, out n);
if (!access || n <= 0)
{
Console.WriteLine("Ошибка ввода");
Console.Write("n = ");
}
else
{
break;
}
}
return n;
}
}
}using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace lab_5_18
{
public class checkdouble
{
public double check_doublearg (int i)
{
bool access;
double n ;
while (true)
{
string s = Console.ReadLine();
access = double.TryParse(s, out n);
if (!access || n <= 0)
{
Console.WriteLine("Ошибка ввода");
Console.Write("a"+ i+1 + " = ");
}
else
{
break;
}
}
return n;
}
}
}using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace lab_5_18
{
class Program
{
static void Main(string[] args)
{
check arg = new check();
checkdouble a = new checkdouble();
double[] mas = new double[1];
int i,n,last,ch = 0,maxi,summ = 0;
double c, max = double.MinValue,d;
Console.WriteLine("Введите число элементов массива");
Console.Write("n = ");
n = arg.chek_values();
Array.Resize<double>(ref mas, n);
Console.WriteLine("Введите число \"C\" ");
Console.Write("C = ");
c = a.check_doublearg(0);
Console.WriteLine("Введите элементы массива через \"Enter\"");
for ( i = 0; i < n; i++)
{
Console.Write("a [" + (i + 1) + "] = ");
mas[i] = a.check_doublearg(i);
if (mas[i] < 0)
{
last = i;
}
else last = -1;
if (mas[i] < c)
{
ch += 1;
}
if (mas [i] > max)
{
max = mas[i];
maxi = i;
}
Console.WriteLine();
}
if (last == -1)
{
}
Console.ReadKey();
}
}
}Решение задачи: «Доступ к переменной, объявленной в теле цикла»
textual
Листинг программы
int i,n,last,ch = 0,maxi,summ = 0;