2012-01-27 67 views
4

已發現的WCF因此開始探索它。 這是一個C#控制檯應用程序。代碼工作正常,除非我嘗試提款。如果我輸入了錯誤類型的數量,它會檢測到(捕獲),通知我輸入無效並將我發送回菜單提示。這很好,很花哨,直到我到達我嘗試提取比我擁有的更多動態的部分(平衡)。據說,我應該收到一條消息,說我沒有足夠的資金撤回。相反,我得到這個:mscorlib中未處理的異常

型「System.FormatException」未處理的異常出現在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; 
    } 
} 

回答

2

您需要量進入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)); 
+0

哦,我的!多麼尷尬......謝謝;) – Bob 2012-01-27 18:16:58

+1

你應該使用Environment.NewLine而不是\ r \ n。 – 2012-01-27 18:18:49

+0

沒問題,發生在我們身上。 – Stephen 2012-01-27 18:19:27

1

你已經在錯誤的地方放了一個圓括號。將您的輸出更改爲:

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