2012-01-27 28 views
4

発見されたWCFが検出されました。 これはC#コンソールアプリケーションです。私が引退しようとするときを除いてコードは正常に動作します。間違ったタイプの金額を入力すると(キャッチ)、無効な入力が通知され、メニュープロンプトに戻ります。それは私が持っているよりも多くのdoshを取り戻そうとする部分に着くまで(バランス)、うまくやっている。おそらく、私はそんなに撤退する資金が足りないというメッセージを受け取ったはずです。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サービスあなたがそう

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

この行にString.Formatの方法に量を移動する必要があります(短い)

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

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

Oh my!どのように恥ずかしい...ありがとう;) – Bob

+1

そして、あなたは\ r \ nの代わりにEnvironment.NewLineを使うべきです。 –

+0

問題はありません、私たちの最高のことが起こります。 – Stephen

1

括弧を間違った場所に入れました。出力を次のように変更します。

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