问题描述
- 怎么写出输入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.num1op.num2op.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;
}
}
解决方案
把代码放到while(true){}里面,当你输入n的时候break就行
解决方案二:
挺简单的,主程序这修改就行。
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""); }