我試圖從用戶處獲取一串字符,並將它們變成電話號碼。在其他方法中使用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()
{
}
嗯,這是一個*實例*變量(即它與你的類型的實例相關聯),但所有的方法都是*靜態*方法 - 你永遠不實際上創建了一個'Program'的實例。 –
你必須使用'ref'嗎?它會更容易,沒有它更乾淨 – Jonesopolis
謝謝selman22和NewHire!它終於編譯完成了! – thetillmiester