C# 错误 ;1和x9;该名称在当前上下文中不存在 类程序 { 静态void Main(字符串[]参数) { int f=0; Console.WriteLine(“输入尝试次数”); int-trycount=Convert.ToInt32(Console.ReadLine()); 随机数=新随机数(); while(f

C# 错误 ;1和x9;该名称在当前上下文中不存在 类程序 { 静态void Main(字符串[]参数) { int f=0; Console.WriteLine(“输入尝试次数”); int-trycount=Convert.ToInt32(Console.ReadLine()); 随机数=新随机数(); while(f,c#,while-loop,int,C#,While Loop,Int,问题是,当我尝试运行该程序时,它会显示名称: “avg”在当前上下文中不存在 发生这种情况的原因以及我如何修复它。您正在循环范围内定义avg,因此它不存在于循环范围之外。(另外,当您将新值指定给avg时,不要替换现有值,而是使用+=增加现有值) 修正: 请参阅中的作用域,以了解在何时何地访问变量和方法。您在“while”的escope中声明了“avg”变量。超出该范围的任何内容都无法看到该变量 您应该在while之外声明avg变量。如果在while循环中声明变量,则会抛出错误,因为“名称“avg

问题是,当我尝试运行该程序时,它会显示名称:

“avg”在当前上下文中不存在


发生这种情况的原因以及我如何修复它。

您正在循环范围内定义
avg
,因此它不存在于循环范围之外。(另外,当您将新值指定给
avg
时,不要替换现有值,而是使用
+=
增加现有值)

修正:

请参阅中的作用域,以了解在何时何地访问变量和方法。您在“while”的escope中声明了“avg”变量。超出该范围的任何内容都无法看到该变量


您应该在while之外声明avg变量。

如果在while循环中声明变量,则会抛出错误,因为“名称“avg”在当前上下文中不存在”。因为您可以在while循环中声明,当while循环中的条件不能满足时,变量无法定义和声明,因此在打印结果时可能会抛出错误

因此,我们可以在while循环之外定义变量

Console.WriteLine(avg/(double)f);
类程序
{
静态void Main(字符串[]参数)
{
int f=0,平均值=0;
Console.WriteLine(“输入尝试次数”);
int-trycount=Convert.ToInt32(Console.ReadLine());
随机数=新随机数();
while(f
类似的内容,请参见注释:

    class Program
    {
        static void Main(string[] args)
        {
             int f = 0,avg=0;
             Console.WriteLine("enter ammount of tries");
             int trycount = Convert.ToInt32(Console.ReadLine());
             Random numgen = new Random();
             while (f < trycount)
             {
                 int now = numgen.Next(1, 6);
                 avg = 0 + now;
                 f++;
             }
             Console.WriteLine(avg);
             Console.ReadLine();
          }
     }

定义超出循环范围的平均值。
Console.WriteLine(avg/(double)f);
    class Program
    {
        static void Main(string[] args)
        {
             int f = 0,avg=0;
             Console.WriteLine("enter ammount of tries");
             int trycount = Convert.ToInt32(Console.ReadLine());
             Random numgen = new Random();
             while (f < trycount)
             {
                 int now = numgen.Next(1, 6);
                 avg = 0 + now;
                 f++;
             }
             Console.WriteLine(avg);
             Console.ReadLine();
          }
     }
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("enter amount of tries"); // typo
        int TryCount = Convert.ToInt32(Console.ReadLine());

        Random numgen = new Random();

        // it is sum we compute in the loop (add up values), not average
        // double: final average will be double: say 2 tries 4 and 5 -> 4.5 avg
        // please notice, that sum is declared out of the loop's scope 
        double sum = 0.0; 

        // for loop looks much more natural in the context then while one
        for (int i = 0; i < TryCount; ++i)
          sum += numgen.Next(1, 6);

        // we want to compute the average, right? i.e. sum / TryCount
        Console.WriteLine(sum / TryCount);
        Console.ReadLine();
    }
}
Console.WriteLine("enter amount of tries"); // typo
int TryCount = Convert.ToInt32(Console.ReadLine());

Random numgen = new Random(); 

Console.Write(Enumerable
  .Range(0, TryCount)
  .Average(x => numgen.Next(1, 6)));

Console.ReadLine();