错误CS1001(标识符预期)
我是编程新手,正在学习C#类。当我尝试写这个程序时,我收到编译器错误CS1001。错误CS1001(标识符预期)
我阅读了编译器错误的描述(下面的链接),但我真的没有得到它。我究竟做错了什么?
http://msdn.microsoft.com/en-us/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);
}
}
根据指示,我应该有一个方法B(InputData),它是直接从方法C(的DataEntry)语句.. ,以下操作的指令:
在图6-24的InputMethodDemo程序的INPUTMETHOD()包含重复 代码提示用户和RET rieves整数值。重写程序,以便 InputMethod()调用另一个方法来完成这项工作。重写的InputMethod() 将只需包含两条语句:
one = DataEntry(“first”);
two = DataEntry(“second”);
保存新程序作为InputMethodDemo2.cs。”
他们指的是InputMethodDemo相同的程序,但它仅调用一个方法(INPUTMETHOD),而不是两个。
我上面提到的文字是“微软的Visual C#®2008年,面向对象的编程介绍,3E,乔伊斯·法雷尔”
任何意见/帮助将不胜感激。
这是你会怎么做:
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;
}
}
更改
public static void InputMethod(out first, out second)
{
one = DataEntry("first");
two = DataEntry("second");
}
到
public static void InputMethod(out DataEntry first, out DataEntry second)
{
first = DataEntry("first");
second = DataEntry("second");
}
您还没有提供的参数的类型。此外,你的论点被称为第一和第二,而不是一个和两个。
这导致了另一个编译错误... – Nooob 2010-10-23 22:03:33
谢谢你的帮助 – Nooob 2010-10-23 22:02:58
这需要本周的家庭作业的照顾。下周有空吗? – 2010-10-24 00:11:10