2010-10-23 384 views
2

我是編程新手,正在學習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,喬伊斯·法雷爾」

任何意見/幫助將不勝感激。

回答

3

這是你會怎麼做:

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

謝謝你的幫助 – Nooob 2010-10-23 22:02:58

+6

這需要本週的家庭作業的照顧。下週有空嗎? – 2010-10-24 00:11:10

1

更改

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

您還沒有提供的參數的類型。此外,你的論點被稱爲第一和第二,而不是一個和兩個。

+0

這導致了另一個編譯錯誤... – Nooob 2010-10-23 22:03:33