它是一个基本的计算器,用户输入两个数字,一个操作和程序使它成为一个方程,并得到答案。例如,如果用户输入数字2,数字3,并告诉它乘,它将显示答案为6,我只是想知道如何改进。
我是否可以使用循环或更高级的程序来获得相同的输出?
namespace Test
{
class Program
{
static void Main(string[] args)
{
int firstNum;
int secondNum; //Variables for equation
string operation;
int answer;
Console.WriteLine("Hello, welcome to Alex's basic calculator!");
Console.ReadLine();
Console.Write("Enter the first number in your basic equation: ");
firstNum = Convert.ToInt32(Console.ReadLine());
//User input for equation
Console.Write("Now enter your second number in the basic equation: ");
secondNum = Convert.ToInt32(Console.ReadLine());
Console.Write("Ok now enter your operation ( x , / , +, -) ");
operation = Console.ReadLine();
if (operation == "x")
{
answer = firstNum * secondNum;
Console.WriteLine(firstNum + " x " + secondNum + " = " + answer);
Console.ReadLine();
}
else if (operation == "/")
{
answer = firstNum / secondNum;
Console.WriteLine(firstNum + " / " + secondNum + " = " + answer);
Console.ReadLine();
}
//Getting answers
else if (operation == "+")
{
answer = firstNum + secondNum;
Console.WriteLine(firstNum + " + " + secondNum + " = " + answer);
Console.ReadLine();
}
else if (operation == "-")
{
answer = firstNum - secondNum;
Console.WriteLine(firstNum + " - " + secondNum + " = " + answer);
Console.ReadLine();
}
else
{
Console.WriteLine("Sorry that is not correct format! Please restart!"); //Catch
Console.ReadLine();
}
}
}
}发布于 2016-06-05 17:52:18
这是个很好的开始..。
请注意,在数字提示符下输入文本(如"hello")将导致异常。有几种方法可以解决这个问题。最简单的方法是使用Int32.TryParse。下面是一个示例:
static int PromptForInteger(string message, int minValue, int maxValue)
{
bool done;
int answer = 0;
// it will run at least once, but we don't know how many times
do
{
// start with an assumption
done = true;
// display the prompt
Console.Write(message);
// check to see if what was entered "looks like" an integer
if (Int32.TryParse(Console.ReadLine(), out answer) == false)
{
// display an error message, and try again
Console.WriteLine("Could not convert your input into a whole number, please try again");
// set our sentinel to indicate a problem
done = false;
} else if (answer < minValue || answer > maxValue)
// if the value within the acceptible range?
{
// display an error message, and try again
Console.WriteLine("Valid values are between {0} and {1}, please try again", minValue, maxValue);
// set our sentinel to indicate a problem
done = false;
}
} while (!done);
// if we make it here, everything is OK
return answer;
}发布于 2016-06-06 13:10:50
首先,你在编写你的第一个程序方面做得很好,我在这里要强调的是,在你未来的程序中,有几件事情是有用的。
注意,我使用了Int32.TryParse()来检查输入值是否可以解析为int,如果不能循环,直到得到有效的输入。开关语句中的缺省值负责操作的验证。根据您想要实现的交互,您可以为操作实现类似的东西,或者在传递不准确的结果时停止程序。
我重构了您的解决方案,并创建了一个使用firstNum、secondNum和操作作为输入的计算方法,并以字符串的形式返回答案。虽然,我试图更改您的代码并使其变得简单。
private static string Calculate(int firstNum, int secondNum, string operation)
{
int answer = 0;
// Switch used as a replacement for if statement
switch (operation)
{
case "x":
answer = firstNum * secondNum;
break;
case "/":
answer = firstNum / secondNum;
break;
case "+":
answer = firstNum + secondNum;
break;
case "-":
answer = firstNum - secondNum;
break;
default:
return "Sorry that is not correct format! Please restart!";
// break;
}
return new StringBuilder(firstNum + operation + secondNum + " = " + answer).ToString();
}注意,我使用的是开关,而不是if语句,您可以在这里找到使用开关的优点,如果为语句的优点,则为切换。
对于良好的实践,最好将打印出来的语句隐藏到主方法中,因为稍后您可能会决定让gui应用程序显示答案并对主方法进行修改比较容易。
注意:我在"Hello,欢迎来到Alex的基本计算器“之后删除了Console.ReadLine(),因为它是多余的,您可以通过询问用户名等来做一些修改。
https://codereview.stackexchange.com/questions/131158
复制相似问题