C# 我的第一台计算器需要帮助吗
所以我刚开始学习编码,已经学了大约一周了。我想试着做一个计算器,可以+和-但是我不知道如何让用户选择他想要使用的东西,有人可以帮我吗? 这是代码C# 我的第一台计算器需要帮助吗,c#,calculator,C#,Calculator,所以我刚开始学习编码,已经学了大约一周了。我想试着做一个计算器,可以+和-但是我不知道如何让用户选择他想要使用的东西,有人可以帮我吗? 这是代码 int x; int y; Console.WriteLine("Welcome to my calculator program!"); Console.WriteLine("This calculator for now can only do + and -"); C
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,如果x大于y,则使用-x。我知道如何让它工作的唯一方法。欢迎来到计算机程序的伟大世界。 应用程序开发就是为用户提供选项并动态处理他/她的请求。 因此,正如您获得x和y的值一样,您可能从用户处获得运算符。一个简单的例子如下:
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");
}
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的值一样,您可能从用户处获得运算符。一个简单的例子如下:
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");
}
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);
}
}
}
}
如果您觉得答案有用,请标记它。首先让我感谢您的尝试,现在我将添加一些注释来改进代码片段。您可以使用
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();
首先让我感谢您的尝试,现在我将添加一些注释来改进代码段。您可以使用
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();
有各种可能的方法来完成你的要求。其中最简单的是要求用户输入操作员进行相应的操作: 下面是相同的代码:
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");
}
}
}
有各种可能的方法来完成你的要求。其中最简单的是要求用户输入操作员进行相应的操作: 下面是相同的代码:
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");
}
}
}
你可以这样走
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();
}
}
你可以这样走
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();
}
}
天真的方法(询问用户)已经得到了回答。但实际上,您可能想要实现的是编写一个完整的计算器,用户在其中输入一个字符串,如2+3
,程序只给出答案。你渴望成为一名翻译
如果您仔细考虑,您将必须执行三项任务:
- 扫描用户输入,并将其转换为一系列令牌。这是将字符串“2+3”转换为标记系列“值为2的整数”、“加号运算符”、“值为3的整数”
- 解释标记以计算结果
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");
}
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);
}
}
}
}
天真的方法(询问用户)已经得到了回答。但实际上,您可能想要实现的是编写一个完整的计算器,用户在其中输入一个字符串,如2+3
,程序只给出答案。你渴望成为一名翻译
如果您仔细考虑,您将必须执行三项任务:
- 扫描用户输入,并将其转换为一系列令牌。这是将字符串“2+3”转换为标记系列“值为2的整数”、“加号运算符”、“值为3的整数”
- 解释标记以计算结果
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");
}
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);
}
}
}
}
也可以从控制台读取字符。然后使用switch case,并根据要求用户写入x和y来执行任务。要求用户写入操作,使用一个开关查找4个操作+,-,*,/中的一个,如果没有选择任何一个,则使用write Console.Log(“无效操作输入”)。另外,当您从控制台获取x和y时,请使用Int或Double-TryParse方法。这将保证用户不能为数字写入E21A,因为您的程序将在当前状态下引发异常。总之,从控制台获取操作,使用TryParse方法,查看如何在发生错误时结束方法的执行(控制台中的无效操作或无效int/decimal)!也可以从控制台读取字符。然后使用switch case,并根据要求用户写入x和y来执行任务。要求用户写入操作,使用一个开关查找4个操作+,-,*,/中的一个,如果没有选择其中任何一个,请写入Console.Log(“无效操作”)