0

사용자로부터 일련의 문자를 가져 와서 전화 번호로 변환하려고합니다.다른 메서드에서 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() 
    { 
    } 
+0

주석이 달린 필드는 해당 인스턴스의 변수와 연결되어 있지만 모든 메서드는 * 정적 * 메서드입니다. 실제로'Program'의 인스턴스를 만듭니다. –

+0

'ref'를 사용해야합니까? 그것 없이는 쉽고 청소기가 될 것입니다. – Jonesopolis

+0

감사합니다. selman22와 NewHire! 그것이 마침내 컴파일 된! – thetillmiester

답변

0

귀하의 방법이 있어야한다.

그런 다음 원하는 전화 :

char1 = Console.ReadKey().KeyChar; 
: 당신의 방법에서

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

당신이 char.Parse 필요하지 않습니다, 대신 당신이 사용자로부터 문자 입력을 얻기 위해 노력하고 생각하는이 같은 수행해야합니다

ref 키워드를 사용하여 매개 변수를 보내는 대신 char으로 돌아가려면 방법을 수정할 수도 있습니다.

0

당신은 매개 변수를 여기에 입력 지정해야합니다,이 컴파일되지 않습니다 :

static void GetInput(ref char1) 

그것은 같은 것을해야한다 :

static void GetInput(ref char char1) 

을 그런 다음 함수에 심판 매개 변수로 변수를 전달하기 전에, 당신은 그것을 초기화해야합니다, 즉 당신은 그것을 defaul 값으로 주어야한다는 것을 의미합니다 :

char char1 = default(char); 

T 암탉은 당신이 당신의 함수에 전달할 수 있습니다

GetInput(ref char1); 

대신 내가 확장 방법으로 내 방법을 정의하고 그 결과 char을 반환 ref를 사용하는 다른 기능에서이 변수를 사용할 경우,이 같은 :

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을 가질 수 있습니다 hat 값은 메소드 외부에 반영되지만 여전히 값이 입력되어야합니다. 사용자가 설명하는 오류는 char1을 초기화하지 않았기 때문입니다. 이 문제를 해결하는 두 가지 쉬운 방법이 있습니다. 첫 번째는 char1 : char char1 = 'x';을 초기화하는 것입니다. 다른 하나는 참조로 전달하는 것에서 출력 매개 변수로 전달하는 것으로 변경하는 것입니다. 목적을 위해서 refoutput의 구별은 메소드가 호출되기 전에 ref가 초기화 될 것으로 기대하고 output은 호출되는 메소드 내 어딘가에서 초기화 될 것으로 기대합니다.

0

ref을 사용해야합니까? 그렇지 않은 경우 GetInput에 void 대신 char이 표시됩니다.

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