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);
}
}
}
}
-9
A
回答
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);
}
}
}
}
相关问题
- 1. C:返回错误的代码风格?
- 2. 我有一些HTML和PHP代码中的一些错误
- 3. 一些错误的C++代码
- 4. 。我想运行一些代码,但错误返回?
- 5. C++ - OpenProcess返回错误代码87
- 6. AngularJS代码返回错误?
- 7. JS代码返回错误
- 8. 返回错误代码
- 9. drupal_http_request返回错误代码
- 10. 我的第一个Django cassandra代码中的一些错误
- 11. 为什么我的Python代码返回一个NZEC错误
- 12. Apache可以返回一个JSON响应404和其他错误?
- 13. 从GWT返回404或其他错误代码
- 14. 其他邮政服务返回错误代码415
- 15. HasLayout(或其他一些IE7)的错误?
- 16. 返回值与返回错误代码?
- 17. glUnmapBuffer - 返回值和错误代码
- 18. 代码中的一些错误 - libpcap
- 19. 我的C代码中的段错误
- 20. 无法修复的Android Proguard的一起返回错误代码1个错误
- 21. 我的代码的另一个错误
- 22. 返回代码错误和错误有什么区别
- 23. 其他语法返回错误
- 24. beforeSave()返回的一些错误
- 25. 我的C代码中的“分段错误”错误
- 26. 返回其他类的对象的C++类getter错误
- 27. Python返回“错误的”退出代码
- 28. 使用的NuSOAP返回错误代码
- 29. 的Linux脚本错误返回代码
- 30. sendmail的返回错误代码为8
这里有什么问题?有什么错误? – Marty
您希望如何从声明为“void”的方法返回值?为什么所有这些参数都是通过参考传递的? –
虚空应该是int,并且我通过引用传递参数,因为那是我老师告诉我要做的。 – lalex