我的第一个计算器需要帮助
本文关键字:帮助 计算器 第一个 我的 | 更新日期: 2023-09-27 18:12:54
所以我刚开始学习编码,一直在它大约1周。我想做一个能做+和-的计算器但不知道如何让用户选择他想用的,有人能帮我吗?代码如下:
int x;
int y;
Console.WriteLine("Welcome to my calculator program!");
Console.WriteLine("This calculator for now can only do + and -");
Console.WriteLine("If x is highter than y it will do - and if x is lower than y it will do +");
Console.WriteLine("pls write in you x");
x = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("pls write in your y");
y = Convert.ToInt32(Console.ReadLine());
if (x > y)
{
Console.WriteLine("you chose to use minus");
int sum = x - y;
Console.WriteLine("result: {0}", sum);
Console.WriteLine("Press a key to exit");
Console.ReadLine();
}
else
{
Console.WriteLine("you chose to use plus");
int sum1 = x + y;
Console.WriteLine("result: {0}", sum1);
Console.WriteLine("Press a key to exit");
Console.ReadLine();
}
}
}
}正如你所看到的,如果x I小于y,就用+,如果x大于y,就用-,这是我知道的唯一可行的方法
有多种可能的方法来完成您所要求的。其中最简单的是要求用户输入相应的操作符:
下面是相同的代码:
using System.IO;
using System;
class Program
{
static void Main()
{
int x;
int y;
Console.WriteLine("Welcome to my calculator program!");
Console.WriteLine("This calculator for now can only do + and -");
// Reads x and y from console.
Console.WriteLine("Enter x :");
x = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter y :");
y = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter operator for corresponding operation on x and y.'nSupported operations x - y and x + y");
string inputOpr = Console.ReadLine(); // Stores operation to perform
// Compares input operator to perform operation.
if (inputOpr == "-"){
Console.WriteLine("you chose to use minus");
int result = x - y;
Console.WriteLine("Result: {0}", result);
Console.WriteLine("Press a key to exit");
Console.ReadLine();
}
else if (inputOpr == "+"){
Console.WriteLine("you chose to use plus");
int result = x + y;
Console.WriteLine("Result: {0}", result);
Console.WriteLine("Press a key to exit");
Console.ReadLine();
}
else{
Console.WriteLine("Invalid Input");
}
}
}
天真的方法(问用户)已经得到了回答。但是你可能真正想要实现的是写一个功能齐全的计算器,用户输入一个字符串,比如2+3
,程序就会给出答案。您希望编写一个解释器。
如果你考虑一下,你将不得不执行三个任务:
- 扫描用户输入,并将其转换为一系列令牌。这是将字符串"2+3"转换为令牌系列"值为2的整数","加运算符","值为3的整数"
- 解释令牌以计算结果
在输入上设置的约束越多(例如,正如您所说,只允许以+和-开始),代码将越简单(如果输入不能根据您设置的规则进行解释,则会失败)。
如果你想深入了解,有很多关于这个主题的资源。我喜欢阅读这一系列的博客文章(作者使用Python,但原理保持不变)。
最后你会发现你想做的很多事情都已经完成了。已经有一些库可以解析和计算数学表达式(例如:NCalc, Mathos)。
最后,Roslyn允许您轻松解析任何c#表达式,因此它也可以用来构建一个简单的计算器,如下所示:class Program
{
static void Main(string[] args)
{
CSharpReplAsync().Wait();
}
private static async Task CSharpReplAsync()
{
Console.WriteLine("C# Math REPL");
Console.WriteLine("You can use any method from System.Math");
var options = ScriptOptions.Default
.AddImports("System", "System.Console", "System.Math");
var state = await CSharpScript.RunAsync("WriteLine('"Hello from Roslyn'")", options);
while (true)
{
Console.Write("> ");
string expression = Console.ReadLine();
if (string.IsNullOrEmpty(expression))
break;
try
{
state = await state.ContinueWithAsync(expression, options);
if (state.ReturnValue != null)
Console.WriteLine(state.ReturnValue);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
欢迎来到计算机程序的伟大世界。应用程序开发就是向用户提供选项并动态处理他/她的请求。因此,就像获取x和y的值一样,也可以从user获取操作符。一个简单的例子是这样的:
int x;
int y;
Console.WriteLine("Welcome to my calculator program!");
Console.WriteLine("This calculator for now can only do + and -");
Console.WriteLine("If x is highter than y it will do - and if x is lower than y it will do +");
Console.WriteLine("pls write in you x");
x = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("pls write in your y");
y = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("insert operator");
string o = Console.ReadLine();
if (o=="-")
{
Console.WriteLine("you chose to use minus");
int sum = x - y;
Console.WriteLine("result: {0}", sum);
Console.WriteLine("Press a key to exit");
Console.ReadLine();
}
else if (o=="+")
{
Console.WriteLine("you chose to use plus");
int sum1 = x + y;
Console.WriteLine("result: {0}", sum1);
Console.WriteLine("Press a key to exit");
Console.ReadLine();
}
else
{
Console.WriteLine("the operator is not recognized");
}
如果你觉得有用,请在答案上做个标记。
首先让我感谢你的尝试,现在我会添加一些注释来改进代码片段。您可以使用int.TryParse
进行转换,这将有助于您处理FormatExceptions
,然后从用户那里获得操作员,并使用开关箱识别并根据用户输入进行操作。
将此代码作为参考,不要复制粘贴
int firstNumber, secondNumber;
Console.WriteLine("pls write in you x");
if (!int.TryParse(Console.ReadLine(), out firstNumber))
{
Console.WriteLine("Sorry this is not an integer, 0 will be assigned");
}
Console.WriteLine("pls write in your y");
if (!int.TryParse(Console.ReadLine(), out secondNumber))
{
Console.WriteLine("Sorry this is not an integer, 0 will be assigned");
}
/ Now you have two numbers to perform the operation
// You can now prompt the user to enter the operator
Console.WriteLine("pls enter the operator");
char operatorSign = Console.ReadKey().KeyChar;
switch (operatorSign)
{
case '+' :
Console.WriteLine("Result {0} + {1} = {2}", firstNumber, secondNumber, firstNumber + secondNumber);
break;
case '_':
Console.WriteLine("Result {0} - {1} = {2}", firstNumber, secondNumber, firstNumber - secondNumber);
break;
case '*':
Console.WriteLine("Result {0} * {1} = {2}", firstNumber, secondNumber, firstNumber * secondNumber);
break;
default:
Console.WriteLine("Sorry we are not providing this operation");
break;
}
Console.ReadKey();
你可以这样写
static void Main(string[] args)
{
int x;
int y;
Console.WriteLine("Welcome to my calculator program!");
Console.WriteLine("This calculator for now can only do + and -");
Console.WriteLine("If x is highter than y it will do - and if x is lower than y it will do +");
Console.WriteLine("pls write in you x");
x = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("pls write in your y");
y = Convert.ToInt32(Console.ReadLine());
string opt;
int res;
do
{
Console.Write("Enter your operator [+/-/:/x]:'t");
opt= Console.ReadLine();
res = "/+/-/:/x/".IndexOf("/" + opt + "/");
} while (res == -1);
double result;
switch (opt)
{
case "+":
result= x + y;
break;
case "-":
result = x - y;
break;
case ":":
result = (double)x/(double)y;
break;
case "x":
result = x*y;
break;
default:
result = -9999;
break;
}
Console.WriteLine("'n{0} {1} {2} = {3}", x, opt, y, result);
Console.ReadLine();
}
}