Noo*_*oob 4 c# identifier method-signature
我是编程新手,正在参加C#课程。尝试编写此程序时出现编译器错误CS1001。
我阅读了“编译器错误”说明(下面的链接),但是我真的没明白。我究竟做错了什么?
http://msdn.microsoft.com/zh-CN/library/b839hwk4.aspx
这是我的源代码:
using System;
public class InputMethodDemoTwo
{
public static void Main()
{
int first, second;
InputMethod(out first, out second);
Console.WriteLine("After InputMethod first is {0}", first);
Console.WriteLine("and second is {0}", second);
}
public static void InputMethod(out first, out second)
// The error is citing the line above this note.
{
one = DataEntry("first");
two = DataEntry("second");
}
public static void DataEntry(out int one, out int two)
{
string s1, s2;
Console.Write("Enter first integer ");
s1 = Console.ReadLine();
Console.Write("Enter second integer ");
s2 = Console.ReadLine();
one = Convert.ToInt32(s1);
two = Convert.ToInt32(s2);
}
}
Run Code Online (Sandbox Code Playgroud)
根据说明,我应该有一个方法b(InputData),它从方法c(DataEntry)中提取语句。以下是说明:
图6-24中InputMethodDemo程序中的InputMethod()包含重复代码,提示用户并检索整数值。重写程序,以便InputMethod()调用另一个方法来完成工作。重写的InputMethod()仅需要包含两个语句:
一个= DataEntry(“ first”);
两个= DataEntry(“ second”);
将新程序另存为InputMethodDemo2.cs。”
他们所指的InputMethodDemo是同一个程序,只是它只调用一个方法(InputMethod),而不是两个。
我上面提到的文本是“Microsoft®Visual C#®2008,面向对象编程的简介,3e,Joyce Farrell”
任何建议/帮助将不胜感激。
这是您应该做的:
using System;
public class InputMethodDemoTwo
{
public static void Main()
{
int first, second;
InputMethod(out first, out second);
Console.WriteLine("After InputMethod first is {0}", first);
Console.WriteLine("and second is {0}", second);
Console.ReadLine();
}
public static void InputMethod(out int first, out int second)
//Data type was missing here
{
first = DataEntry("first");
second = DataEntry("second");
}
public static int DataEntry(string method)
//Parameter to DataEntry should be string
{
int result = 0;
if (method.Equals("first"))
{
Console.Write("Enter first integer ");
Int32.TryParse(Console.ReadLine(), out result);
}
else if (method.Equals("second"))
{
Console.Write("Enter second integer ");
Int32.TryParse(Console.ReadLine(), out result);
}
return result;
}
}
Run Code Online (Sandbox Code Playgroud)