这是我的第一篇文章,为我可能犯的错误和糟糕的格式化道歉。
我遇到的问题是它第二次循环 int whichAccount = int.Parse(Console.ReadLine());不起作用,不会接受我的意见。它引发了异常“输入字符串格式不正确”。它第一次循环它一切正常。我做错了什么?感谢。
class ATM
{
const int SAVING_ACCOUNT = 1;
const int DEBIT_CARD = 2;
const int CREDIT_CARD = 3;
const int INVESTMENT_ACCOUNT = 4;
static double[] accountBalances = { 0.0, 1001.45, 850.0, -150.0, 10000.0 };
static string[] accountNames = { "", "Savings Account", "Debit Card",
"Credit Card", "Investment Account" };
static void Main()
{
char y;
do {
Console.Write("\tSAVING_ACCOUNT = 1;\n\tDEBIT_CARD = 2;\n\tCREDIT_CARD = 3;\n\tINVESTMENT_ACCOUNT = 4;\n\nPlease select account: ");
int whichAccount = Int32.Parse(Console.ReadLine());
DisplayBalance(whichAccount);
Console.Write("\nDo you wish to see the balance of another account? Y/N: ");
y = (char)Console.Read();
} while (Char.IsLetter(y));
}
static void DisplayBalance(int whichAccount)
{
switch (whichAccount)
{
case 1: Console.WriteLine("\nAccount Balance of Savings Account = ${0}", accountBalances[1]);
DateTime date = DateTime.Now;
Console.WriteLine("Current Date: {0} ", date );
break;
case 2: Console.WriteLine("{0}", accountBalances[2]);
break;
case 3: Console.WriteLine("{0}", accountBalances[3]);
break;
case 4: Console.WriteLine("{0}", accountBalances[4]);
break;
}
}
答案 0 :(得分:0)
问题归因于y = (char)Console.Read();
考虑使用以下内容来读取输入中的第一个字符。它将确保读取整个输入,不会在剩余的控制台文本中留下任何其他字符或空格:
y = Console.ReadLine().Trim()[0];