【发布时间】:2014-03-28 06:35:57
【问题描述】:
我正在尝试从用户那里获取一串字符并将它们转换为电话号码。
我们还没有在课堂上讨论过数组,所以我不想在这个程序中使用类似的东西。我对他们也不太了解。我们复习了传递参考,但我并没有真正理解它。
我的问题是如何在其他方法中使用我的 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;
2/25/14 当我使用它时,它只会输入第一个字符。我可以输入 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()
{
}
【问题讨论】:
-
好吧,你注释掉的字段是一个 instance 变量(即它与你的类型的实例相关联),但你所有的方法都是 static方法 - 你永远不会真正创建
Program的实例。 -
一定要用
ref吗?没有它会更容易,更清洁 -
感谢 selman22 和 NewHire!终于编译成功了!
标签: c# variables methods console-application pass-by-reference