2 qq 34418970 qq_34418970 于 2016.05.04 12:27 提问

怎么写出输入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;
}
}

2个回答

herozhangbz
herozhangbz   2016.05.04 13:02

把代码放到while(true){}里面,当你输入n的时候break就行

qq_34418970
qq_34418970 学的不是很好,怎么放都不会成功,赐教
一年多之前 回复
herozhangbz
herozhangbz   2016.05.04 18: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");
            }
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!