gpt4 book ai didi

c# - mscorlib 中未处理的异常

转载 作者:行者123 更新时间:2023-11-30 12:14:39 25 4
gpt4 key购买 nike

发现了 WCF 并因此开始探索它。这是一个 C# 控制台应用程序。代码工作正常,除非我尝试退出。如果我输入错误类型的数量,它会检测到(捕获),通知我输入无效并将我送回菜单提示。这很好而且花花公子,直到我到达我尝试提取比我拥有的(平衡)更多的 dosh 的部分。据说,我应该得到消息说我资金短缺,无法提取这么多。相反,我得到这个:

An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

我哪里出错了?

主要

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace BankAccountClient
{
class Program
{
static void Main(string[] args)
{
BankAccountClient.ServiceReference1.ServiceClient client = new ServiceReference1.ServiceClient();
bool done = false;
do
{
Console.WriteLine("Select one of the following:");
Console.WriteLine("\t1 -- Withdraw");
Console.WriteLine("\t2 -- Deposit");
Console.WriteLine("\t3 -- Balance");
Console.Write("Enter Your selection (0 to exit): ");
string strSelection = Console.ReadLine();
int iSel;
try
{
iSel = int.Parse(strSelection);
}
catch (FormatException)
{
Console.WriteLine("\r\nWhat?\r\n");
continue;
}
Console.WriteLine("\nYou selected " + iSel + "\n");
switch (iSel)
{
case 0:
done = true;
break;
case 1:
int balance = client.Balance();
int amount;

//WCF Withdraw
Console.Write("How much would you like to withdraw?: ");
try
{
amount = int.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("\r\nInvalid input. Must be an integer\r\n");
continue;
}
if (amount > balance)
{
Console.WriteLine(String.Format("\r\nNot enough funds to withdraw ${0}\r\n"), amount);
continue;
}
else
client.Withdraw(amount);
Console.WriteLine(String.Format("\nCurrent balance is ${0}\n", client.Balance()));
break;
case 2:
//WCF Deposit
Console.WriteLine("Deposit();");
break;
case 3:
//WCF Balance
Console.WriteLine(String.Format("Current balance is ${0}", client.Balance()));
break;
default:
Console.WriteLine("You selected an invalid number: {0}\r\n", iSel);
continue;
}
Console.WriteLine();
} while (!done);

Console.WriteLine("\nGoodbye!");
}
}
}

WCF 服务(短)

public class Service : IService
{

private static int balance;

public void Withdraw(int value)
{
balance -= value;
}

public void Deposit(int value)
{
balance += value;
}

public int Balance()
{
return balance;
}
}

最佳答案

您需要将 amount 移动到这一行的 String.Format 方法中

Console.WriteLine(String.Format("\r\nNot enough funds to withdraw ${0}\r\n"), amount);

所以

Console.WriteLine(String.Format("\r\nNot enough funds to withdraw ${0}\r\n", amount));

关于c# - mscorlib 中未处理的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9037825/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com