사용자로부터 일련의 문자를 가져 와서 전화 번호로 변환하려고합니다.다른 메서드에서 char 변수 사용 * 편집 됨 *
아직 클래스에 배열을 전달하지 않았으므로이 프로그램에서 이와 같은 것을 사용하고 싶지 않습니다. 나는 아직 그들에 대해서 많이 모른다. 우리는 지나가는 참조를 건너 갔지만, 나는 그것을 실제로 이해하지 못했습니다.
내 질문에 내 char 변수를 다른 방법으로 어떻게 사용합니까? 변수를 클래스 밑에 넣으려고 했는데도 변수가 작동하지 않습니다. 수신하는 가장 일반적인 오류 메시지는 다음과 같습니다.
비 정적 필드, 메서드에 개체 참조가 필요합니다. 나는이 만 1 입력 한 문자를 얻을 그것은 사용하는 경우
그래서 나는이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;
2/25/14 갔다 :
여기 내 코드입니다. 나는 2 7을 입력 할 수 있지만 모든 문자를 쓰려고 할 때 첫 번째 문자 만 씁니다. 그 이유는 무엇입니까? 이 문제를 어떻게 해결할 수 있습니까? 당신은 매개 변수의 데이터 형식을 누락
static void GetInput(ref char char1)
{
Console.WriteLine("Enter your first character:");
char1 = Console.ReadKey().KeyChar;
}
: 새로운 코드 : 같은
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