4

我是编程新手,正在学习 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");

Save the new program as InputMethodDemo2.cs."

The InputMethodDemo they are referring to is the same program, except that it calls only one method (the InputMethod) instead of two.

The text I referred to above is "Microsoft® Visual C#® 2008, An Introduction to Object-Oriented Programming, 3e, Joyce Farrell"

Any advice/ help would be greatly appreciated.

4

2 回答 2

5

This is what you are expected to do:

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;
    }
}
于 2010-10-23T21:50:36.773 回答
2

Change

public static void InputMethod(out first, out second)
{
  one = DataEntry("first");     
  two = DataEntry("second");
}

to

public static void InputMethod(out DataEntry first, out DataEntry second)
{
  first = DataEntry("first"); 
  second = DataEntry("second");
}

You haven't provided the type of the arguments. Also, your arguments are called first and second, not one and two.

于 2010-10-23T21:33:16.617 回答