2016-01-31 53 views
-9
namespace PrePaidPhone 
{ 
    class Program 
    { 
     //Method (takes seven char reference parameters and reuturns void) 
     static void GetInput(ref char N1, ref char N2, ref char N3, ref char N4, ref char N5, ref char N6, ref char N7) 
     { 
      //Get 7 characters from the user and store them in the 7 variables that main() has passed by reference 
      Console.WriteLine("Enter a 7 character phone number: "); 
      N1 = Convert.ToChar(Console.Read()); 
      N2 = Convert.ToChar(Console.Read()); 
      N3 = Convert.ToChar(Console.Read()); 
      N4 = Convert.ToChar(Console.Read()); 
      N5 = Convert.ToChar(Console.Read()); 
      N6 = Convert.ToChar(Console.Read()); 
      N7 = Convert.ToChar(Console.Read()); 

      Console.ReadLine(); 
     } 

     //Method (takes seven char reference parameters and returns an integer) 
     static void ProcessInput(ref int N1, ref int N2, ref int N3, ref int N4, ref int N5, ref int N6, ref int N7) 
     { 
      //Call ToDigit() for each of the 7 characters 
      //If toDigit returns an error code (-1), return an error code (-1) 
      //You will need to do this part highlighted in yellow 7 times, once for each of the seven character variable that were passed from main. 
      //If toDigit returns an error code (-1), return an error code (-1) 
      if (ToDigit(N1 == -1) 
       return -1; 
      if (N2 == -1) 
       return -1; 
      if (N3 == -1) 
       return -1; 
      if (N4 == -1) 
       return -1; 
      if (N5 == -1) 
       return -1; 
      if (N6 == -1) 
       return -1; 
      if (N7 == -1) 
       return -1; 
      //You will need to do this part highlighted in yellow 7 times, once for each of the seven character variable that were passed from main. 
      //If the first character is 0, return an error code (-1) to Main() 
      if (N1 == 0) 
       return -1; 
      //If the first three characters are 555, return an error code (-1) 
      if (N1 == '5' && N2 == '5' && N3 =='5') 
       return -1; 
      //If there are no errors, return 0 
       return 0; 
     } 

     //Method (takes one char reference parameter, returns an integer) 
     static void ToDigit(ref char digit) 
     { 
      //Convert the characters (passed from ProcessInput() by reference) to upper case 
      //Use a switch statement to translate characters into their corresponding digits 
      digit = char.ToUpper(digit); 
      //Write a case statement for each digit and for each valid uppercase letter 
      switch(digit) 
      { 
       case '0': digit = '0'; 
        break; 
       case '1': digit = '1'; 
        break; 

       case '2': 
       case 'A': 
       case 'B': 
       case 'C': digit = '2'; 
        break; 

       case '3': 
       case 'D': 
       case 'E': 
       case 'F': digit = '3'; 
        break; 

       case '4': 
       case 'G': 
       case 'H': 
       case 'I': digit = '4'; 
        break; 

       case '5': 
       case 'J': 
       case 'K': 
       case 'L': digit = '5'; 
        break; 

       case '6': 
       case 'M': 
       case 'N': 
       case 'O': digit = '6'; 
        break; 

       case '7': 
       case 'P': 
       case 'Q': 
       case 'R': 
       case 'S': digit = '7'; 
        break; 

       case '8': 
       case 'T': 
       case 'U': 
       case 'V': digit ='8'; 
        break; 

       case '9': 
       case 'W': 
       case 'X': 
       case 'Y': 
       case 'Z': digit = '9'; 
        break; 
       //Write a default case that returns an error code (-1) for invalid letters 
       default: return -1; 
      } 
      //If there are no invalid letters, return 0 this goes after the switch statement 
      return 0; 
     } 

     //Method (takes 7 char parameters, returns void) 
     static void ShowResults(ref char N1, ref char N2, ref char N3, ref char N4, ref char N5, ref char N6, ref char N7) 
     { 
      //Display the Phone Number using the character variables Main() has passed by reference 
      Console.WriteLine("The phone number is : {1}{2}{3}{4}{5}{6}{7}" + N1 + N2 + N3 + "-" + N4 + N5 + N6 + N7); 
     } 

     //Main() Method 
     static void Main(string[] args) 
     { 

      //Declare 7 char variables and one integer for the result from ProcessInput() 
      char N1 = 'A', N2 = 'A', N3 = 'A', N4 = 'A', N5 = 'A', N6 = 'A', N7 = 'A'; 

      //Get user input by calling the GetInput() method, passing it the 7 variables by reference 
      GetInput(ref N1, ref N2, ref N3, ref N4, ref N5, ref N6, ref N7); 

      //Perform the conversion by calling the ProcessInput() method, passing it the 7 variables by reference.  
      //Assign the result to an integer variable 
      int outputNumber = ProcessInput(ref N1, ref N2, ref N3, ref N4, ref N5, ref N6, ref N7);  

      //Display an error message or call ShowResults(), depending on the code ProcessInput() returns 
      if (outputNumber == -1)       
      { 
       //Output that there was an error 
       Console.WriteLine("Invalid input, please try again."); 
      } 
      else //Otherwise 
      { 
       //Call ShowResults() 
       ShowResults(ref N1, ref N2, ref N3, ref N4, ref N5, ref N6, ref N7); 
      } 

     } 
    } 
} 
+5

这里有什么问题?有什么错误? – Marty

+1

您希望如何从声明为“void”的方法返回值?为什么所有这些参数都是通过参考传递的? –

+0

虚空应该是int,并且我通过引用传递参数,因为那是我老师告诉我要做的。 – lalex

回答

2

那么我们都已经在那里。下面是你的代码与我的意见有关的错误的原因:

namespace PrePaidPhone 
{ 
    class Program 
    { 
     //Method (takes seven char reference parameters and reuturns void) 
     static void GetInput(ref char N1, ref char N2, ref char N3, ref char N4, ref char N5, ref char N6, ref char N7) 
     { 
      //Get 7 characters from the user and store them in the 7 variables that main() has passed by reference 
      Console.WriteLine("Enter a 7 character phone number: "); 
      N1 = Convert.ToChar(Console.Read()); 
      N2 = Convert.ToChar(Console.Read()); 
      N3 = Convert.ToChar(Console.Read()); 
      N4 = Convert.ToChar(Console.Read()); 
      N5 = Convert.ToChar(Console.Read()); 
      N6 = Convert.ToChar(Console.Read()); 
      N7 = Convert.ToChar(Console.Read()); 

      Console.ReadLine(); 
     } 

     //Method (takes seven char reference parameters and returns an integer) 
     static void ProcessInput(ref int N1, ref int N2, ref int N3, ref int N4, ref int N5, ref int N6, ref int N7) 
     { 
      //Call ToDigit() for each of the 7 characters 
      //If toDigit returns an error code (-1), return an error code (-1) 
      //You will need to do this part highlighted in yellow 7 times, once for each of the seven character variable that were passed from main. 
      //If toDigit returns an error code (-1), return an error code (-1) 

      /* 
       Errors below are for two reasons, 
       1. ToDigit expects char but you are getting Ints 
       2. Return type of function is void, but you are trying to return some value. either change return type to int or 
       don't return anything. In your case it will be better to return true/false and change void to bool 
      */ 
      ////////////////////////////////// 
      //Should be ToDigit(ref N1)==-1 // 
      ////////////////////////////////// 
      if (ToDigit(N1== -1) 
       return -1; 
      if (N2 == -1) 
       return -1; 
      if (N3 == -1) 
       return -1; 
      if (N4 == -1) 
       return -1; 
      if (N5 == -1) 
       return -1; 
      if (N6 == -1) 
       return -1; 
      if (N7 == -1) 
       return -1; 
      //You will need to do this part highlighted in yellow 7 times, once for each of the seven character variable that were passed from main. 
      //If the first character is 0, return an error code (-1) to Main() 
      if (N1 == 0) 
       return -1; 
      //If the first three characters are 555, return an error code (-1) 
      if (N1 == '5' && N2 == '5' && N3 =='5') 
       return -1; 
      //If there are no errors, return 0 
       return 0; 
     } 

     //Method (takes one char reference parameter, returns an integer) 
     static void ToDigit(ref char digit) 
     { 
      //Convert the characters (passed from ProcessInput() by reference) to upper case 
      //Use a switch statement to translate characters into their corresponding digits 
      digit = char.ToUpper(digit); 
      //Write a case statement for each digit and for each valid uppercase letter 
      switch(digit) 
      { 
       case '0': digit = '0'; 
        break; 
       case '1': digit = '1'; 
        break; 

       case '2': 
       case 'A': 
       case 'B': 
       case 'C': digit = '2'; 
        break; 

       case '3': 
       case 'D': 
       case 'E': 
       case 'F': digit = '3'; 
        break; 

       case '4': 
       case 'G': 
       case 'H': 
       case 'I': digit = '4'; 
        break; 

       case '5': 
       case 'J': 
       case 'K': 
       case 'L': digit = '5'; 
        break; 

       case '6': 
       case 'M': 
       case 'N': 
       case 'O': digit = '6'; 
        break; 

       case '7': 
       case 'P': 
       case 'Q': 
       case 'R': 
       case 'S': digit = '7'; 
        break; 

       case '8': 
       case 'T': 
       case 'U': 
       case 'V': digit ='8'; 
        break; 

       case '9': 
       case 'W': 
       case 'X': 
       case 'Y': 
       case 'Z': digit = '9'; 
        break; 
       //Write a default case that returns an error code (-1) for invalid letters 
       default: return -1; ///YOU CANOT RETURN ANYTHING FROM void FUNCTION 
      } 
      //If there are no invalid letters, return 0 this goes after the switch statement 
      return 0; ///YOU CANOT RETURN ANYTHING FROM void FUNCTION 
     } 

     //Method (takes 7 char parameters, returns void) 
     static void ShowResults(ref char N1, ref char N2, ref char N3, ref char N4, ref char N5, ref char N6, ref char N7) 
     { 
      //Display the Phone Number using the character variables Main() has passed by reference 
      Console.WriteLine("The phone number is : {1}{2}{3}{4}{5}{6}{7}" + N1 + N2 + N3 + "-" + N4 + N5 + N6 + N7); 
     } 

     //Main() Method 
     static void Main(string[] args) 
     { 

      //Declare 7 char variables and one integer for the result from ProcessInput() 
      char N1 = 'A', N2 = 'A', N3 = 'A', N4 = 'A', N5 = 'A', N6 = 'A', N7 = 'A'; 

      //Get user input by calling the GetInput() method, passing it the 7 variables by reference 
      GetInput(ref N1, ref N2, ref N3, ref N4, ref N5, ref N6, ref N7); 

      //Perform the conversion by calling the ProcessInput() method, passing it the 7 variables by reference.  
      //Assign the result to an integer variable 

      /////////////////////////////////////////////////////////////////// 
      // ProcessInput expects ints, but N1..7 are ints so these errors // 
      ///////////////////////////////////////////////////////////////////   
      int outputNumber = ProcessInput(ref N1, ref N2, ref N3, ref N4, ref N5, ref N6, ref N7); 

      //Display an error message or call ShowResults(), depending on the code ProcessInput() returns 
      if (outputNumber == -1)       
      { 
       //Output that there was an error 
       Console.WriteLine("Invalid input, please try again."); 
      } 
      else //Otherwise 
      { 
       //Call ShowResults() 
       ShowResults(ref N1, ref N2, ref N3, ref N4, ref N5, ref N6, ref N7); 
      } 
     } 
    } 
} 
+0

谢谢你的帮助。我知道我很模糊,但我甚至不知道问题是什么,甚至试图问。 – lalex

+0

谢谢你的帮助。我知道我很模糊,但我甚至不知道问题是什么,甚至试图问。我认真地认为你只是保存了我的理智,当我不工作的时候,我一直在努力工作。这是我正在上的一堂课。 – lalex