【问题标题】:type or namespace definition, or end-of-file expected. } expected类型或命名空间定义,或预期的文件结尾。 } 预期的
【发布时间】:2015-09-19 02:07:44
【问题描述】:

我试图找到解决方案,但没有找到。

问题似乎是公共 int 号,但我不知道如何解决它。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PrimTal
{
    class Program
    {
        public static void Main(string[] args)
        { // } expected

            public int number; 
            number = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Ditt nummer är: " + number);

            Console.ReadKey();
        }
    }
} // type or namespace definition, or end-of-file expected

【问题讨论】:

  • 您不能在内部方法中使用publicint number; 就够了,或者把 public int number; 移到上课开始的地方

标签: c#


【解决方案1】:

属性必须在“外部”,如下所示:

class Program
{
    // Properties
    public static int Number {get; set;}

    // Methods
    public static void Main(string[] args)
    { 

        Number = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Ditt nummer är: " + Number);

        Console.ReadKey();
    }
}

或者您可以使用局部变量:int number = Convert.ToInt32(Console.ReadLine()); in Main

【讨论】:

  • 让您的财产static
  • @X-TECH 你是对的 X-TECH。我编辑了它。感谢您的评论。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-16
  • 1970-01-01
  • 1970-01-01
  • 2021-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多