0

我是 C# 新手,我正在尝试让一个基本的 I/O 程序来工作,询问用户的姓名和年龄。该程序不允许我运行,因为“static void GetUserData()”部分给出了这个错误:

Expected class, delegate, enum, interface, or struct. 
It does not like the void keyword.

GetUserData() 方法也给出了错误Does not exist in current context。据我所知,这遵循 C# 中的规则,我被声明为稍后使用的方法,并且声明是在一个类中进行的,所以应该可以吗?

谢谢大家,我把它整理好了。我完全按照书上所说的方式输入了程序,无论如何这是一个教训。


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

namespace BasicConsoleIO
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("basic input output");
            GetUserData();
            Console.ReadLine();
        }
    }

    static void GetUserData()
    {
    Console.Write("please enter your name: ");
    string userName = Console.ReadLine();
    Console.Write("please enter your age: ");
    string userAge = Console.ReadLine();

    //changes echo colour
    ConsoleColor prevColor = Console.ForegroundColor;
    Console.ForegroundColor = ConsoleColor.red;

    //echo to console
    Console.WriteLine("hello {0}! Your are {1} years old.", userName, userAge);

    //Restore previous color
    Console.ForegroundColor = prevColor;
    }   
}
4

6 回答 6

1

上一堂课GetUserData(),例如你现在上的课;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("basic input output");
        GetUserData();
        Console.ReadLine();
    }

    static void GetUserData()
    {
        Console.Write("please enter your name: ");
        string userName = Console.ReadLine();
        Console.Write("please enter your age: ");
        string userAge = Console.ReadLine();

        //changes echo colour
        ConsoleColor prevColor = Console.ForegroundColor;
        Console.ForegroundColor = ConsoleColor.red;

        //echo to console
        Console.WriteLine("hello {0}! Your are {1} years old.", userName, userAge);

        //Restore previous color
        Console.ForegroundColor = prevColor;
    }   
}

此外,由于它们位于相同的命名空间和类中,如果您只需要从那里使用它们,则无需将您的方法设置为静态,这完全取决于您希望如何访问它们,或者更重要的是,您想从哪里访问它们

于 2013-07-01T11:31:41.107 回答
0

方法必须在类中

namespace BasicConsoleIO
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("basic input output");
            GetUserData();
            Console.ReadLine();
        }

        static void GetUserData()
        {
            Console.Write("please enter your name: ");
            string userName = Console.ReadLine();
            Console.Write("please enter your age: ");
            string userAge = Console.ReadLine();

            //changes echo colour
            ConsoleColor prevColor = Console.ForegroundColor;
            Console.ForegroundColor = ConsoleColor.Red;

            //echo to console
            Console.WriteLine("hello {0}! Your are {1} years old.", userName, userAge);

            //Restore previous color
            Console.ForegroundColor = prevColor;
        }
    }
}
于 2013-07-01T11:48:50.373 回答
0

您的方法需要在一个类中,如下所示:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("basic input output");
        GetUserData();
        Console.ReadLine();
    }

    static void GetUserData()
    {
        ...
    }
}
于 2013-07-01T11:29:45.997 回答
0

在 C# 中,每个方法都必须一个类中。

你已经定义了你的程序

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("basic input output");
        GetUserData();
        Console.ReadLine();
    }
}

然后GetUserData在右大括号之后定义 - 将其移动到类定义中。

于 2013-07-01T11:31:40.863 回答
0

GetUserData()方法超出类的范围Program。应该:

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("basic input output");
            GetUserData();
            Console.ReadLine();
        }

        static void GetUserData() {/*code here*/}
    }
于 2013-07-01T11:30:48.273 回答
0

基本上在 C# 中,代码应该包含在一个类中。可以通过将 GetUserData() 移动到程序类来解决该错误

class Program
{
    static void Main(string[] args)
    {
     Console.WriteLine("basic input output");
     GetUserData();
     Console.ReadLine();
    }

 static void GetUserData()
  {
    Console.Write("please enter your name: ");
    string userName = Console.ReadLine();
    Console.Write("please enter your age: ");
    string userAge = Console.ReadLine();

    //changes echo colour
    ConsoleColor prevColor = Console.ForegroundColor;
    Console.ForegroundColor = ConsoleColor.red;

    //echo to console
    Console.WriteLine("hello {0}! Your are {1} years old.", userName, userAge);

    //Restore previous color
    Console.ForegroundColor = prevColor;
  }   
}
于 2016-05-05T10:09:41.160 回答