想飞的鱼丶 2016-05-04 04:27 采纳率: 0%
浏览 2970

怎么写出输入Y/N继续运算

class Operation
{
public int num1;
public int num2;

}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入连个整数");
OperationNum op = new OperationNum();
op.num1 = Convert.ToInt32(Console.ReadLine());
op.num2 = Convert.ToInt32(Console.ReadLine());
// int num1 = Convert.ToInt32(Console.ReadLine());
// int num2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请选择运算符 \n1.加法\n2.减法\n3.乘法\n4.除法 ");

int s = 0;
s = Convert.ToInt32(Console.ReadLine());
switch (s)
{
case 1:
Console.WriteLine("{0}+{1}={2}",op.num1,op.num2,op.Add());

break;
case 2:
Console.WriteLine("差为{0}", op.Sub());
break;
case 3:
Console.WriteLine("积为{0}", op.Mul());
break;
case 4:
Console.WriteLine("商为{0}", op.Mul());
break;

}

}
}
class OperationNum : Operation
{
public int Add()
{
return num1 + num2;
}
public int Sub()
{
return num1 - num2;
}
public int Mul()
{
return num1 * num2;
}
public int Div()
{
return num1 + num2;
}
}

  • 写回答

3条回答 默认 最新

  • herozhangbz 2016-05-04 10:35
    关注

    挺简单的,主程序这修改就行。

         while (true)
                {
                    Console.WriteLine("请输入两个整数");
                    OperationNum op = new OperationNum();
                    op.num1 = Convert.ToInt32(Console.ReadLine());
                    op.num2 = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("请选择运算符 \n1.加法\n2.减法\n3.乘法\n4.除法 ");
                    int s = 0;
                    s = Convert.ToInt32(Console.ReadLine());
                    switch (s)
                    {
                        case 1:
                            Console.WriteLine("{0}+{1}={2}", op.num1, op.num2, op.Add());
                            break;
                        case 2:
                            Console.WriteLine("差为{0}", op.Sub());
                            break;
                        case 3:
                            Console.WriteLine("积为{0}", op.Mul());
                            break;
                        case 4:
                            Console.WriteLine("商为{0}", op.Div());
                            break;
                        default:
                            break;
                    }
                    Console.WriteLine("请输入Y/N继续或退出");
                    string yesorno = Console.ReadLine();
                    if (yesorno == "Y" || yesorno == "y")
                        continue;
                    else if (yesorno == "N" || yesorno == "n")
                        break;
                    else
                        Console.WriteLine("error");
                }
    

    或者

                bool ck = true;
                while (ck)
                {
                    Console.WriteLine("请输入两个整数");
                    OperationNum op = new OperationNum();
                    op.num1 = Convert.ToInt32(Console.ReadLine());
                    op.num2 = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("请选择运算符 \n1.加法\n2.减法\n3.乘法\n4.除法 ");
                    int s = 0;
                    s = Convert.ToInt32(Console.ReadLine());
                    switch (s)
                    {
                        case 1:
                            Console.WriteLine("{0}+{1}={2}", op.num1, op.num2, op.Add());
                            break;
                        case 2:
                            Console.WriteLine("差为{0}", op.Sub());
                            break;
                        case 3:
                            Console.WriteLine("积为{0}", op.Mul());
                            break;
                        case 4:
                            Console.WriteLine("商为{0}", op.Div());
                            break;
                        default:
                            break;
                    }
                    Console.WriteLine("请输入Y/N继续或退出");
                    string yesorno = Console.ReadLine().ToLower();
                    if (yesorno == "y")
                        continue;
                    else if (yesorno == "n")
                        ck = false;
                    else
                        Console.WriteLine("error");
                }
    
    评论

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用