【问题标题】:Error CS1001 (Identifier expected)错误 CS1001(需要标识符)
【发布时间】:2010-10-23 21:29:49
【问题描述】:

我是编程新手,正在学习 C# 课程。当我尝试编写此程序时,我收到编译器错误 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()包含重复 提示用户并检索整数值的代码。重写程序,使 InputMethod() 调用另一个方法来完成这项工作。重写的 InputMethod() 只需要包含两个语句:

one = DataEntry("first");

two = DataEntry("second");

将新程序另存为 InputMethodDemo2.cs。"

他们所指的 InputMethodDemo 是同一个程序,只是它只调用一个方法(InputMethod)而不是两个。

我上面提到的文字是“Microsoft® Visual C#® 2008, An Introduction to Object-Oriented Programming, 3e, Joyce Farrell”

任何建议/帮助将不胜感激。

【问题讨论】:

    标签: c# identifier method-signature


    【解决方案1】:

    这是你应该做的:

    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;
        }
    }
    

    【讨论】:

    • 这周的功课已经完成了。下周有货吗?
    【解决方案2】:

    改变

    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");
    }
    

    您尚未提供参数的类型。此外,您的论点被称为第一个和第二个,而不是一个和两个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多