2012-01-27 16 views
4

발견 된 WCF가 검색되었으므로 검색을 시작했습니다. 이것은 C# 콘솔 응용 프로그램입니다. 철회를 시도 할 때를 제외하고 코드는 정상적으로 작동합니다. 잘못된 유형의 금액을 입력하면 (catch) 잘못된 입력을 알리고 메뉴 프롬프트로 돌아갑니다. 그것은 내가 균형 잡힌 것보다 더 많은 돈을 인출하려고하는 부분에 도달 할 때까지 훌륭하고 멋장이다. 기꺼이 돈을 많이 모으지 않는다는 메시지를 받았어 야합니다.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 서비스 (짧은)

당신이 줄에 및 String.format 방법에

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

정도 양을 이동해야

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

오 마이! 얼마나 당황 스럽습니까 ... 고마워요;) – Bob

+1

그리고 \ r \ n 대신 Environment.NewLine을 사용해야합니다. –

+0

아무 문제 없어, 우리에게 최고입니다. – Stephen

1

잘못된 위치에 괄호를 넣었습니다. 출력을 다음으로 변경하십시오.

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