Gee*_*eek 0 c# function calculator console-application
在用户选择选项后,如何使此程序继续运行?这是我到目前为止的代码.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace calculator_extended
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Press A for addition");
Console.WriteLine("Press S for subtraction");
Console.WriteLine("Press M for Multiplication");
Console.WriteLine("Press D for Divide");
char c = Convert.ToChar(Console.ReadLine());
int a = Convert.ToInt32(Console.ReadLine());
int b = Convert.ToInt32(Console.ReadLine());
switch (c)
{
case 'A':
case 'a':
{
int d = add(a, b);
Console.WriteLine(d);
break;
}
case 'S':
case 's':
{
int d = sub(a, b);
Console.WriteLine(d);
break;
}
case 'M':
case 'm':
{
int d = mul(a, b);
Console.WriteLine(d);
break;
}
case 'E':
case 'e':
{
int d = div(a, b);
Console.WriteLine(d);
break;
}
default:
{
Console.WriteLine("Please Enter the correct Character");
break;
}
}
}
private static int add(int a, int b)
{
return a + b;
}
private static int sub(int a, int b)
{
return a - b;
}
private static int mul(int a, int b)
{
return a * b;
}
private static int div(int a, int b)
{
return a / b;
}
}
}
Run Code Online (Sandbox Code Playgroud)
用你的代码包装
while(true) {
// Your code here
// Don't forget to add a switch case for an Exit operation
case 'q':
Application.Exit();
}
Run Code Online (Sandbox Code Playgroud)
如果你是编程新手,你应该研究循环 - 参见清单4-2 - 这是你想要完成的一个很好的例子.
编辑:我知道你是编程新手,我不认为你应该靠自己做到这一点.由于这是一个基本问题,这是一个完整的解决方案
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace calculator_extended
{
class Program
{
static void Main(string[] args)
{
int d = 0;
while (true)
{
Console.WriteLine("Press A for addition");
Console.WriteLine("Press S for subtraction");
Console.WriteLine("Press M for Multiplication");
Console.WriteLine("Press D for Divide");
char c = Convert.ToChar(Console.ReadLine());
int a = Convert.ToInt32(Console.ReadLine());
int b = Convert.ToInt32(Console.ReadLine());
switch (c)
{
case 'A':
case 'a':
d = add(a, b);
Console.WriteLine(d);
break;
case 'S':
case 's':
d = sub(a, b);
Console.WriteLine(d);
break;
case 'M':
case 'm':
d = mul(a, b);
Console.WriteLine(d);
break;
case 'E':
case 'e':
d = div(a, b);
Console.WriteLine(d);
break;
case 'q':
case 'Q':
Application.Exit();
default:
Console.WriteLine("Please Enter the correct Character");
break;
}
}
}
private static int add(int a, int b)
{
return a + b;
}
private static int sub(int a, int b)
{
return a - b;
}
private static int mul(int a, int b)
{
return a * b;
}
private static int div(int a, int b)
{
return a / b;
}
}
}
Run Code Online (Sandbox Code Playgroud)