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

指示によると、メソッド c (DataEntry) からステートメントを取得するメソッド b (InputData) があるはずです... 指示は次のとおりです。

図 6-24 の InputMethodDemo プログラムの InputMethod() には、ユーザーにプロンプ​​トを表示して整数値を取得する反復コードが含まれています。InputMethod() が別のメソッドを呼び出して作業を行うように、プログラムを書き直してください。書き直された InputMethod() には、次の 2 つのステートメントのみを含める必要があります。

one = DataEntry("最初");

two = DataEntry("秒");

新しいプログラムを InputMethodDemo2.cs として保存します。」

彼らが参照している InputMethodDemo は、2 つではなく 1 つのメソッド (InputMethod) のみを呼び出すことを除いて、同じプログラムです。

上記で参照したテキストは、「Microsoft® Visual C#® 2008、オブジェクト指向プログラミング入門、3e、Joyce Farrell」です。

アドバイス/ヘルプをいただければ幸いです。

4

2 に答える 2

5

これはあなたがすることが期待されていることです:

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

変化する

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

引数の型を指定していません。また、引数は、1 と 2 ではなく、1 番目と 2 番目と呼ばれます。

于 2010-10-23T21:33:16.617 に答える