2012-04-14 101 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

当抛出异常时,您可以读取异常的消息以及查看异常的堆栈跟踪。应该帮助你解决这样的问题。如果你仍然不明白问题是什么,那么至少应该在你的问题中包含这些信息。 – 2012-04-14 11:47:36

+0

这是一个家庭作业吗?你应该给它一个适当的标签。 – Turbot 2012-04-14 14:25:46

回答

4

int.Parse(Console.ReadLine())要求您输入一个有效的整数。如果您只是按下return而不输入值,则该方法会抛出异常。改为使用int.TryParse

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

对不起,只是很好,默认情况下做到了:-) – Matten 2012-04-14 11:53:15

+0

@Matten谢谢。 OP处理交换机默认分支内的无效条目。这就是为什么我跳过使用返回值。 – 2012-04-14 11:54:35