2012-04-14 8 views
-1

、我々は...、ユーザが入力し、彼/彼女の貯蓄情報の詳細を取得するアプリケーションを作成するように求めていたコンソールアプリケーションは、私たちの先輩の要求時に例外

その事実ことを除いて 、完全に正常に動作私はEnterキーを押すと例外が発生し、プログラムがクラッシュします。

私のコンソールアプリケーションで、私がEnterキーを押しても、代わりに例外が出るのではなく、プログラムを返すように情報を提供してください。

ありがとう、 非常に助けてください。

これは私のプログラムです。

using System; 

using System.Collections.Generic; 

using System.Text; 



namespace bankprob { 



    class sav_acc 

    { 

     public float amount; 

     public sav_acc(float amount) 
     { 

      this.amount = amount; 

     } 

     public void getdeposit(float depos) 
     { 

      amount += depos; 

     } 

     public void display() 
     { 

      Console.WriteLine("Balance of Customer :{0} ", amount); 

     } 


     public void withdrawl(float amt) 
     { 

      amount =amount - amt; 

     } 

     public void minbal() 
     { 

      if (amount < 1000) 
      { 

       Console.WriteLine("You cannot withdraw beyond the minimum balance of rupees 1000. "); 
       return; 
      } 

     } 

    } 

    class cur_acc 
    { 

     public float amount = 0; 

     public cur_acc(float amount) 


     { 

      this.amount = amount; 

     } 

     public void getdeposit(float depos) 
     { 

      amount += depos; 

     } 

     public void display() 
     { 

      Console.WriteLine("Balance of Customer : {0}", amount); 

     } 


     public void withdrawl(float amt) 
     { 

      amount = amount - amt; 

     } 

     public void minbal() 
     { 

      if (amount < 1000) 
      { 

       Console.WriteLine(" Your balance is less than 1000, and u cannot make any withdrawals"); 

      } 

      else 

       Console.WriteLine("Balance is greater than Rs 1000 no need to panality"); 

     } 

    } 

    class Program 
    { 

     public static void Main(string[] args) 
     { 
      Console.WriteLine("Welcome Mr.Sayeed"); 
      Console.WriteLine("Please select the type of account.\n1.Savings 2.Current 3.Exit"); 

      int ch; 
      ch = int.Parse(Console.ReadLine()); 
      switch (ch) 
      { 

       case 1: 

        Console.WriteLine("Enter Initail Amount : "); 

        float amt = int.Parse(Console.ReadLine()); 

        sav_acc s = new sav_acc(amt); 

        Console.WriteLine("Enter deposit money : "); 

        float depos = float.Parse(Console.ReadLine()); 

        s.getdeposit(depos); 

        s.display(); 

        Console.WriteLine("Enter withdrawl Amount"); 

        float wamt = float.Parse(Console.ReadLine()); 

        s.withdrawl(wamt); 

        s.display(); 

        s.minbal(); 

        s.display(); 

        break; 

       case 2: 

        Console.WriteLine("Enter Initail Amount : "); 

        float am = int.Parse(Console.ReadLine()); 

        cur_acc c = new cur_acc(am); 

        Console.WriteLine("Enter deposit money : "); 

        float depo = float.Parse(Console.ReadLine()); 

        c.getdeposit(depo); 

        c.display(); 


        Console.WriteLine("Enter withdrawl Amount"); 

        float wam = float.Parse(Console.ReadLine()); 

        c.withdrawl(wam); 

        c.display(); 

        c.minbal(); 

        c.display(); 

        break; 
       case 3: 
        Console.WriteLine("Thank you for Using this applicaton"); 
        return; 

       default: 
        Console.WriteLine("You have made a wrong choice, Thank you..."); 
        return; 

      } 

     } 

    } 

} 
+1

例外がスローされると、例外のメッセージを読み、例外のスタックトレースを見ることができます。あなたはこのような問題を解決するのに役立ちます。問題の内容をまだ理解していない場合は、質問にその情報を含める必要があります。 –

+0

これは宿題ですか?適切なタグを付けてください。 – Turbot

答えて

4

int.Parse(Console.ReadLine())には、有効な整数を入力する必要があります。値を入力せずにreturnキーを押すだけで、このメソッドは例外をスローします。代わりにint.TryParseを使用してください。

int ch = 0; 
int.TryParse(Console.ReadLine(), ch); 
switch (ch) { 
    ... 
} 
+0

申し訳ありませんが、デフォルトのケースはそれでした:-) – Matten

+0

@マットンありがとう。 OPは、スイッチのデフォルトブランチの内部で無効なエントリを処理します。だから私は戻り値を使用してスキップしました。 –

関連する問題