2014-02-25 32 views
0

我試圖從用戶處獲取一串字符,並將它們變成電話號碼。在其他方法中使用char變量*編輯*

我們沒有走在班上陣列尚未所以我不想在這個程序中使用這樣的事情。我也不太瞭解他們。我們通過了引用,但我並沒有真正理解它。

我的問題是如何在我的其他方法中使用我的char變量?我曾嘗試將該變量放在課堂上,但也沒有奏效。我收到的最常見的錯誤信息是:需要用於非靜態字段,方法

的對象引用。

這裏是我的代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace Damian_CIS170B_Lab4 
{ 
    class Program 
    { 
     //char char1; 

     static void Main(string[] args) 
     { 
      Console.WriteLine("****Phone Dialing Program****\n"); 

      char char1; 

       //char2, char3, char4, char5, char6, char7; 

      GetInput(ref char1); 
      ProcessInput(); 
      ToDigit(); 
      ShowResults(); 

      Console.Read(); 
     } 

     static void GetInput(ref char1) 
     { 
      Console.WriteLine("Enter your first character:"); 
      Console.ReadLine() = char.Parse(char1); 

      /* Console.WriteLine("Enter your second character:"); 
       Console.WriteLine("Enter your third character:"); 
       Console.WriteLine("Enter your fourth character:"); 
       Console.WriteLine("Enter your fifth character:"); 
       Console.WriteLine("Enter your sixth character:"); 
       Console.WriteLine("Enter your seventh character:"); */ 
     }  

     static void ProcessInput() 
     { 
     } 

     static void ToDigit() 
     { 
     } 

     static void ShowResults() 
     { 
     } 
    } 
} 

所以我這個

  Console.WriteLine("Enter your first character:"); 
      char1 = Console.ReadKey().KeyChar; 

14年2月25日 去當我用這個,只得到第一個輸入的字符。我能夠輸入2到7,但是當我試圖讓它寫出所有的字符時,它只寫第一個字符,爲什麼?我怎樣才能解決這個問題? 新代碼:

static void Main(string[] args) 
    { 
     Console.WriteLine("****Phone Dialing Program****\n"); 

     char char1 = default(char); 
     char char2 = default(char); 
     char char3 = default(char); 
     char char4 = default(char); 
     char char5 = default(char); 
     char char6 = default(char); 
     char char7 = default(char); 


     GetInput(ref char1, char2, char3, char4, char5, char6, char7); 
     ProcessInput(ref char1, char2, char3, char4, char5, char6, char7); 
     //ToDigit(ref char1, char2, char3, char4, char5, char6, char7); 
     ShowResults(); 


     Console.Read(); 
    } 

    static void GetInput(ref char char1, char char2, char char3, char char4, char char5, char char6, char char7) 
    { 
     Console.WriteLine("Enter your first character:"); 
     char1 = Console.ReadKey().KeyChar; 

     Console.WriteLine("\nEnter your second character:"); 
     char2 = Console.ReadKey().KeyChar; 

     Console.WriteLine("\nEnter your third character:"); 
     char3 = Console.ReadKey().KeyChar; 

     Console.WriteLine("\nEnter your fourth character:"); 
     char4 = Console.ReadKey().KeyChar; 

     Console.WriteLine("\nEnter your fifth character:"); 
     char5 = Console.ReadKey().KeyChar; 

     Console.WriteLine("\nEnter your sixth character:"); 
     char6 = Console.ReadKey().KeyChar; 

     Console.WriteLine("\nEnter your seventh character:"); 
     char7 = Console.ReadKey().KeyChar; 

     ToDigit(ref char1, char2, char3, char4, char5, char6, char7); 

    } 

    static void ProcessInput(ref char char1, char char2, char char3, char char4, char char5, char char6, char char7) 
    { 
     char[] chars = { char1, char2, char3, char4, char5, char6, char7 }; 
     string enteredChars = new string(chars); 

     //This is me just trying to see if it is working... its not :(
     Console.WriteLine("This is what you entered: {0}", enteredChars); 

    } 

    static void ToDigit(ref char char1, char char2, char char3, char char4, char char5, char char6, char char7) 
    { 
     switch(char1) 
     { 
      case 'A': 
      case 'a': 
      case 'B': 
      case 'b': 
      case 'C': 
      case 'c': 
      case '2': Console.WriteLine("\n2"); 
       break; 
      default: Console.WriteLine("\n"); 
       break; 
     } 

    } 

    static void ShowResults() 
    { 
    } 
+0

嗯,這是一個*實例*變量(即它與你的類型的實例相關聯),但所有的方法都是*靜態*方法 - 你永遠不實際上創建了一個'Program'的實例。 –

+0

你必須使用'ref'嗎?它會更容易,沒有它更乾淨 – Jonesopolis

+0

謝謝selman22和NewHire!它終於編譯完成了! – thetillmiester

回答

0

你的方法應該是這樣的:

static void GetInput(ref char char1) 
{ 
    Console.WriteLine("Enter your first character:"); 
    char1 = Console.ReadKey().KeyChar; 
} 

你缺少的參數的數據類型。

然後調用它像:

char1 = Console.ReadKey().KeyChar; 

char char1 = default(char); 
GetInput(ref char1); 

在你的方法,你不需要char.Parse,而是我相信你正在試圖從用戶輸入的字符,應該像做

您還可以修改你的方法來獲取char回來,而不是用ref關鍵字發送參數。

0

您應該指定的參數類型在這裏,這不會編譯:

static void GetInput(ref char1) 

它應該是這樣的:

static void GetInput(ref char char1) 

然後你傳遞一個變量作爲ref參數的函數之前,你應該初始化它,這意味着你應該給它一個預設值:

char char1 = default(char); 

T母雞,你可以把它傳遞給你的函數:

GetInput(ref char1); 

如果你想在其他功能使用這個變量,而不是使用ref我會定義我的方法爲擴展方法,並返回結果char,是這樣的:

public static class MyCharExtensions 
{ 
    public static char GetInput() 
    { 
     return Console.ReadKey().KeyChar; 
    } 
    public static char ProcessInput(this char source) 
    { 
     // do your work with char and return it 
    } 
    public static char ToDigit(this char source) 
    { 
     // do your work with char and return it 
    } 
    public static void ShowResults(this char source) 
    { 

    } 
} 

,並利用它們是這樣的:

MyCharExtensions.GetInput().ProcessInput().ShowResults(); 
0

按引用傳遞讓你的方法來改變價值和個人有T帽子值反映在方法之外,但它仍然需要有一個值進來。你所描述的錯誤是因爲你還沒有初始化char1。有兩種簡單的方法可以解決這個問題,第一種方法是初始化char1:char char1 = 'x';。另一個是從引用傳遞變爲傳遞輸出參數。爲了您的目的,refoutput之間的區別在於,ref期望在調用該方法之前將該值初始化,並且輸出期望它在被調用的方法內的某處進行初始化。

0

您是否需要使用ref?如果沒有,只需要GetInput返回一個char而不是void。這可能是你一個開始:

static void Main(string[] args) 
    { 
     Console.WriteLine("****Phone Dialing Program****\n"); 

     char first = GetInput("first"); 
     char second = GetInput("second"); 
     char third = GetInput("third"); 
     char fourth = GetInput("fourth"); 
     char fifth = GetInput("fifth"); 
     char sixth = GetInput("sixth"); 
     char seventh = GetInput("seventh"); 
     char eighth = GetInput("eigth"); 

     Console.Write(new string(new []{first,second,third,fourth,fifth,sixth,seventh,eighth})); 

     Console.Read(); 
    } 

    static char GetInput(string count) 
    { 
     Console.WriteLine("Enter your " + count + " character:"); 
     return Console.ReadLine()[0]; 
    }  
你註釋掉場
+0

我無法得到這個工作......我同意這會讓我的代碼看起來更清潔。 – thetillmiester

相關問題