A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 吴承烨 中级黑马   /  2013-6-19 02:15  /  925 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. <font size="4">using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;

  5. namespace 用面向对象做计算器
  6. {
  7.     class Program
  8.     {
  9.         //全局变量
  10.         public static int numBer1;
  11.         public static string sign;
  12.         public static int numBer2;

  13.         static void Main(string[] args)
  14.         {
  15.             //用面向对象写计算器
  16.             //封装,继承,多态
  17.             while (true)
  18.             {
  19.                 try
  20.                 {
  21.                     Console.WriteLine("请您输入数字");
  22.                     numBer1 = Convert.ToInt32(Console.ReadLine()); //第一个数字
  23.                 }
  24.                 catch
  25.                 {
  26.                     Console.WriteLine("您输入的数字错误,请重新输入");
  27.                     numBer1 = Convert.ToInt32(Console.ReadLine()); //第一个数字
  28.                 }
  29.                 try
  30.                 {
  31.                     Console.WriteLine("请您输入字符");
  32.                     sign = Console.ReadLine();                   //字符
  33.                 }
  34.                 catch
  35.                 {
  36.                     Console.WriteLine("您输入的字符错误,请重新输入");
  37.                     sign = Console.ReadLine();   
  38.                 }
  39.                 try
  40.                 {
  41.                     Console.WriteLine("请您输入数字");
  42.                     numBer2 = Convert.ToInt32(Console.ReadLine()); //第二个数字
  43.                     break;
  44.                 }
  45.                 catch
  46.                 {
  47.                     Console.WriteLine("您输入的数字错误,请重新输入");
  48.                     numBer2 = Convert.ToInt32(Console.ReadLine()); //第二个数字
  49.                 }
  50.             }
  51.             //继承+方法名一样
  52.             Count count = new Count();  //遍历父类
  53.             switch (sign)
  54.             {
  55.                 case "+": count = new Add(); //赋值给父类
  56.                     break;
  57.                 case "-": count = new Reduce();
  58.                     break;
  59.                 case "*": count = new Ride();
  60.                     break;
  61.                 case "/": count = new Divide();
  62.                     break;
  63.             }
  64.             //Add add = new Add();
  65.             //Count count = add;
  66.             //计算
  67.             int output = 0;
  68.             output = count.UseCount(numBer1, numBer2);
  69.             Console.WriteLine("{0}{1}{2}={3}", numBer1, sign, numBer2, output);
  70.             Console.ReadKey();
  71.         }
  72.     }
  73.     //父类
  74.     class Count
  75.     {
  76.         public virtual int UseCount(int number1, int number2)
  77.         {
  78.             return 0;
  79.         }
  80.     }
  81.     //加子类
  82.     class Add : Count
  83.     {
  84.         public override int UseCount(int number1, int number2)
  85.         {
  86.             return (number1 + number2);
  87.         }
  88.     }
  89.     //减子类
  90.     class Reduce : Count
  91.     {
  92.         public override int UseCount(int number1, int number2)
  93.         {
  94.             return (number1 - number2);
  95.         }
  96.     }
  97.     //乘类
  98.     class Ride : Count
  99.     {
  100.         public override int UseCount(int number1, int number2)
  101.         {
  102.             return (number1 * number2);
  103.         }
  104.     }
  105.     //除类
  106.     class Divide : Count
  107.     {
  108.         public override int UseCount(int number1, int number2)
  109.         {
  110.             return (number1 / number2);
  111.         }
  112.     }
  113. }
  114. </font>
复制代码

评分

参与人数 1技术分 +1 收起 理由
苏波 + 1

查看全部评分

1 个回复

倒序浏览
numBer1 = Convert.ToInt32(Console.ReadLine()); //第一个数字;    numBer2 = Convert.ToInt32(Console.ReadLine()); //第二个数字  都抛异常说明输入的不是一个数字,不能进行转换了,您这两句让catch情何以堪 啊!!!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马