我是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;
}
}
答案 0 :(得分: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;
}
}
此外,由于它们位于相同的命名空间和类中,如果您只是从那里需要它们,则不需要将您的方法设置为静态,这一切都取决于您希望如何访问他们或更多,你想从
访问它们答案 1 :(得分:0)
您的方法需要在类中,如下所示:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("basic input output");
GetUserData();
Console.ReadLine();
}
static void GetUserData()
{
...
}
}
答案 2 :(得分:0)
GetUserData()
方法超出了类Program
的范围。应该是:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("basic input output");
GetUserData();
Console.ReadLine();
}
static void GetUserData() {/*code here*/}
}
答案 3 :(得分:0)
在C#中,每个方法必须在类中。
您已定义程序
class Program
{
static void Main(string[] args)
{
Console.WriteLine("basic input output");
GetUserData();
Console.ReadLine();
}
}
然后在结束括号后定义GetUserData
- 将其移到类定义中。
答案 4 :(得分: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;
}
}
}
答案 5 :(得分: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;
}
}