[英]I'm Trying to Build a Simple Calculator in C# Console
using System;
namespace Calculator
{
class Program
{
static void Main(string[] args)
{
int num1, num2;
Console.WriteLine("Enter a operation: Add, Subtract, Multiply, divide");
Console.ReadLine();
Console.WriteLine("Enter a number");
num1 = int.Parse(Console.ReadLine());
Console.WriteLine("Enter another number");
num2 = int.Parse(Console.ReadLine());
}
static int Operations(int add, int subtract, int multiply, int divide, int operation, int answer, int num1, int num2)
{
if (operation == add)
{
add = num1 + num2;
answer = add;
Console.WriteLine(answer);
return answer;
}
else if (operation == subtract)
{
subtract = num1 - num2;
answer = subtract;
Console.WriteLine(answer);
return answer;
}
else if (operation == multiply)
{
multiply = num1 * num2;
answer = subtract;
Console.WriteLine(answer);
return answer;
}
else if (operation == divide)
{
if (num2 == 0)
{
Console.WriteLine("Cannot Compute");
}
}
else
{
divide = num1 / num2;
answer = divide;
Console.WriteLine(answer);
return answer;
}
return answer;
}
}
}
Hi, I'm new to C# I started it like a year and a half ago, and haven't touched it since.嗨,我是 C# 的新手,我大约在一年半前开始使用它,此后就再也没有碰过它。 I got back into it like a week ago and I want to make a calculator as my first "major project", and I can't seem to make it work properly.
一周前我又回到了它,我想做一个计算器作为我的第一个“主要项目”,但我似乎无法让它正常工作。 I'm expecting it to where the user can input an operation and enter 2 numbers to either add, subtract, multiply, or divide, but I can't seem to make the console print out the answer to anything.
我期待它在用户可以输入操作并输入 2 个数字以进行加、减、乘或除的地方,但我似乎无法让控制台打印出任何问题的答案。 I made it where you can input the operation you want and the 2 numbers.
我在你可以输入你想要的操作和 2 个数字的地方做了它。 But it's not printing out the answer to the calculations.
但它并没有打印出计算的答案。 What can I do to make it work?
我该怎么做才能让它发挥作用?
using System;
namespace Calculator
{
class Program
{
static int num1, num2;
static string operation;
static int add;
static int answer;
static int subtract;
static int multiply;
static int divide;
static void Main(string[] args)
{
Program program = new Program();
Console.WriteLine("Enter a operation: Add, Subtract, Multiply, divide");
operation = Console.ReadLine();
Console.WriteLine("Enter a number");
num1 = int.Parse(Console.ReadLine());
Console.WriteLine("Enter another number");
num2 = int.Parse(Console.ReadLine());
program.Operations();
Console.ReadKey();
}
public void Operations()
{
if (operation == "add")
{
add = num1 + num2;
answer = add;
Console.WriteLine("\nanswer: " + answer);
return;
}
else if (operation == "subtract")
{
subtract = num1 - num2;
answer = subtract;
Console.WriteLine("\nanswer: " + answer);
return;
}
else if (operation == "multiply")
{
multiply = num1 * num2;
answer = subtract;
Console.WriteLine("\nanswer: " + answer);
return;
}
else if (operation == "divide")
{
divide = num1 / num2;
answer = divide;
Console.WriteLine("\nanswer: " + answer);
return;
}
else
{
if (num2 == 0)
{
Console.WriteLine("Cannot Compute");
}
}
return;
}
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.