C#如何循环回到计算器中的第一行代码?
您好,我正在尝试编写一个科学计算器,我是c#新手,我在尝试实现一个选项时遇到了一些问题,在计算结束时,我可以询问用户是否要重新启动计算器 这是我目前得到的:C#如何循环回到计算器中的第一行代码?,c#,C#,您好,我正在尝试编写一个科学计算器,我是c#新手,我在尝试实现一个选项时遇到了一些问题,在计算结束时,我可以询问用户是否要重新启动计算器 这是我目前得到的: namespace Scientific_Calculator_Project { class SuperCalculator { static void Main(string[] args) { // Declaring my variables
namespace Scientific_Calculator_Project
{
class SuperCalculator
{
static void Main(string[] args)
{
// Declaring my variables
double num1, num2, answer;
string operation;
string exitOption;
// Asking the user for the operation, to call a function to perform that operation
Console.Write("Please enter an operation you wish to perform (+, -, /, *, ^, ^1/2, !, f-1): ");
operation = Console.ReadLine();
// Prompting the input of the first number
Console.Write("Please enter your first number: ");
num1 = Convert.ToDouble(Console.ReadLine());
// Prompting the input of the second number
Console.Write("Please enter your second number (if no other number is needed please enter ): ");
num2 = Convert.ToDouble(Console.ReadLine());
// Using if statements to decide which function to call based on the operation, as well as an error message for an invalid value
if (operation == "+")
{
Console.WriteLine(Sum(num1, num2));
}
else if (operation == "-")
{
Console.WriteLine(Minus(num1, num2));
}
else if (operation == "/")
{
Console.WriteLine(Divide(num1, num2));
}
else if (operation == "*")
{
Console.WriteLine(Multi(num1, num2));
}
else if (operation == "^")
{
Console.WriteLine(ToPower(num1, num2));
}
else if (operation == "^1/2")
{
Console.WriteLine(Sqroot(num1));
}
else if (operation == "!")
{
Console.WriteLine(Factorial(num1));
}
else if (operation == "f-1")
{
Console.WriteLine(ToInverse(num1));
}
else if (operation == "log")
{
Console.WriteLine(ToLog(num1));
}
else if (operation == "rad")
{
Console.WriteLine(ToRadian(num1));
}
else
{
Console.WriteLine("Invalid operation");
}
Console.WriteLine("Would you like to continue? (Yes/No)");
exitOption = Console.ReadLine();
Console.ReadLine();
// Function for addition (Sum)
static double Sum(double num1, double num2)
{
double resultofSum = num1 + num2;
return resultofSum;
}
// Function for subtraction (Minus)
static double Minus(double num1, double num2)
{
double resultofMinus = num1 - num2;
return resultofMinus;
}
// Function for division (Divide)
static double Divide(double num1, double num2)
{
double resultofDivide = num1 / num2;
return resultofDivide;
}
// Function for multiplication (Multi)
static double Multi(double num1, double num2)
{
double resultofMulti = num1 * num2;
return resultofMulti;
}
// Function for raising x (num1) to the power of y (num2)
static double ToPower(double num1, double num2)
{
double resultofToPower = Math.Pow(num1, num2);
return resultofToPower;
}
// Function for square root of x (num1)
static double Sqroot(double num1)
{
double resultofSqroot = Math.Sqrt(num1);
return resultofSqroot;
}
// Function for finding factorial of x (num1),
static double Factorial(double num1)
{
double factorial = 1;
if (num1 < 0)
{
Console.WriteLine("Error: Can't find factorial of a negative number");
return 0;
}
else if (num1 <= 1)
{
return 1;
}
else
{
for (double i = 1; i <= num1; i++)
{
factorial = factorial * i;
}
Console.WriteLine("{0}! = {1}", num1, factorial);
return factorial;
}
}
// Function for obtaining the inverse of x (1/num1)
static double ToInverse(double num1)
{
double ToInverse = 1 / num1;
return ToInverse;
}
// Function for obtaining the base 10 log of x (num1)
static double ToLog(double num1)
{
double Tolog = Math.Log10(num1);
return Tolog;
}
// Function for converting an angle x (num1) from degrees to radians
static double ToRadian(double num1)
{
double Toradian = (num1 * (Math.PI)) / 180;
return Toradian;
}
}
}
}
namespace-Scientific\u-Calculator\u项目
{
类超级计算器
{
静态void Main(字符串[]参数)
{
//声明我的变量
双num1,num2,答案;
串操作;
串出;
//请求用户执行该操作,调用函数以执行该操作
控制台。写入(“请输入您希望执行的操作(+、-、/、*、^、^1/2、!、f-1):”;
operation=Console.ReadLine();
//提示输入第一个数字
控制台。写下(“请输入您的第一个号码:”);
num1=Convert.ToDouble(Console.ReadLine());
//提示输入第二个数字
控制台。写下(“请输入您的第二个号码(如果不需要其他号码,请输入):”;
num2=Convert.ToDouble(Console.ReadLine());
//使用if语句根据操作决定调用哪个函数,以及无效值的错误消息
如果(操作==“+”)
{
控制台写入线(总和(num1,num2));
}
else if(操作=“-”)
{
控制台写入线(减(num1,num2));
}
else if(操作==“/”)
{
控制台写入线(除法(num1,num2));
}
else if(操作==“*”)
{
控制台写入线(多(num1,num2));
}
else if(操作==“^”)
{
控制台写入线(ToPower(num1,num2));
}
否则如果(操作==“^1/2”)
{
Console.WriteLine(Sqroot(num1));
}
else if(操作==“!”)
{
Console.WriteLine(阶乘(num1));
}
否则如果(操作==“f-1”)
{
Console.WriteLine(ToInverse(num1));
}
else if(操作==“日志”)
{
控制台写入线(ToLog(num1));
}
else if(操作==“rad”)
{
控制台写入线(ToRadian(num1));
}
其他的
{
Console.WriteLine(“无效操作”);
}
Console.WriteLine(“您想继续吗?(是/否)”);
exitOption=Console.ReadLine();
Console.ReadLine();
//加法函数(和)
静态双和(双num1,双num2)
{
双结果总和=num1+num2;
返回结果总和;
}
//减法函数(减法)
静态双负(双num1,双num2)
{
双结果负=num1-num2;
返回负数;
}
//除法功能(除法)
静态双除法(双num1,双num2)
{
双结果除法=num1/num2;
返回结果进行分割;
}
//乘法函数(多重)
静态双多(双num1,双num2)
{
double resultofMulti=num1*num2;
返回结果;
}
//将x(num1)提升为y(num2)幂的函数
静态双电源(双num1,双num2)
{
双结果功率=数学功率(num1,num2);
返回结果topower;
}
//x的平方根函数(num1)
静态双平方根(双num1)
{
double resultofSqroot=Math.Sqrt(num1);
返回resultofSqroot;
}
//用于查找x(num1)的阶乘的函数,
静态双阶乘(双num1)
{
双因子=1;
if(num1<0)
{
WriteLine(“错误:找不到负数的阶乘”);
返回0;
}
else if(num1有很多种方法,但这里是一种方法的示例。下面的代码将在用户每次点击一个键时循环,除非他们点击“回车”键,在这种情况下,它将退出并结束:
static void Main(string[] args)
{
ConsoleKeyInfo keyInfo;
bool loop = true;
while (loop)
{
//do your stuff here
//...
//then do this:
keyInfo = Console.ReadKey();
if(keyInfo.Key == ConsoleKey.Enter)
{
loop = false;
}
else
{
loop = true;
}
}
}
请参阅下面的更改
using System;
namespace Scientific_Calculator_Project
{
class SuperCalculator
{
static void Main(string[] args)
{
// Declaring my variables
double num1, num2, answer;
string operation;
string exitOption;
while (true)
{
// Asking the user for the operation, to call a function to perform that operation
Console.Write("Please enter an operation you wish to perform (+, -, /, *, ^, ^1/2, !, f-1): ");
operation = Console.ReadLine();
// Prompting the input of the first number
Console.Write("Please enter your first number: ");
num1 = Convert.ToDouble(Console.ReadLine());
// Prompting the input of the second number
Console.Write("Please enter your second number (if no other number is needed please enter ): ");
num2 = Convert.ToDouble(Console.ReadLine());
// Using if statements to decide which function to call based on the operation, as well as an error message for an invalid value
if (operation == "+")
{
Console.WriteLine(Sum(num1, num2));
}
else if (operation == "-")
{
Console.WriteLine(Minus(num1, num2));
}
else if (operation == "/")
{
Console.WriteLine(Divide(num1, num2));
}
else if (operation == "*")
{
Console.WriteLine(Multi(num1, num2));
}
else if (operation == "^")
{
Console.WriteLine(ToPower(num1, num2));
}
else if (operation == "^1/2")
{
Console.WriteLine(Sqroot(num1));
}
else if (operation == "!")
{
Console.WriteLine(Factorial(num1));
}
else if (operation == "f-1")
{
Console.WriteLine(ToInverse(num1));
}
else if (operation == "log")
{
Console.WriteLine(ToLog(num1));
}
else if (operation == "rad")
{
Console.WriteLine(ToRadian(num1));
}
else
{
Console.WriteLine("Invalid operation");
}
Console.WriteLine("Would you like to continue? (Yes/No)");
exitOption = Console.ReadLine();
if(exitOption.ToLower() == "no")
{
break;
}
// Function for addition (Sum)
static double Sum(double num1, double num2)
{
double resultofSum = num1 + num2;
return resultofSum;
}
// Function for subtraction (Minus)
static double Minus(double num1, double num2)
{
double resultofMinus = num1 - num2;
return resultofMinus;
}
// Function for division (Divide)
static double Divide(double num1, double num2)
{
double resultofDivide = num1 / num2;
return resultofDivide;
}
// Function for multiplication (Multi)
static double Multi(double num1, double num2)
{
double resultofMulti = num1 * num2;
return resultofMulti;
}
// Function for raising x (num1) to the power of y (num2)
static double ToPower(double num1, double num2)
{
double resultofToPower = Math.Pow(num1, num2);
return resultofToPower;
}
// Function for square root of x (num1)
static double Sqroot(double num1)
{
double resultofSqroot = Math.Sqrt(num1);
return resultofSqroot;
}
// Function for finding factorial of x (num1),
static double Factorial(double num1)
{
double factorial = 1;
if (num1 < 0)
{
Console.WriteLine("Error: Can't find factorial of a negative number");
return 0;
}
else if (num1 <= 1)
{
return 1;
}
else
{
for (double i = 1; i <= num1; i++)
{
factorial = factorial * i;
}
Console.WriteLine("{0}! = {1}", num1, factorial);
return factorial;
}
}
// Function for obtaining the inverse of x (1/num1)
static double ToInverse(double num1)
{
double ToInverse = 1 / num1;
return ToInverse;
}
// Function for obtaining the base 10 log of x (num1)
static double ToLog(double num1)
{
double Tolog = Math.Log10(num1);
return Tolog;
}
// Function for converting an angle x (num1) from degrees to radians
static double ToRadian(double num1)
{
double Toradian = (num1 * (Math.PI)) / 180;
return Toradian;
}
}
}
}
}
使用系统;
名称空间科学计算器项目
{
类超级计算器
{
静态void Main(字符串[]参数)
{
//声明我的变量
双num1,num2,答案;
串操作;
串出;
while(true)
{
//请求用户执行该操作,调用函数以执行该操作
控制台。写入(“请输入您希望执行的操作(+、-、/、*、^、^1/2、!、f-1):”;
operation=Console.ReadLine();
//提示输入第一个数字
控制台。写下(“请输入您的第一个号码:”);
num1=Convert.ToDouble(Console.ReadLine());
//提示输入第二个数字
控制台。写下(“请输入您的第二个号码(如果不需要其他号码,请输入):”;
num2=Convert.ToDouble(Console.ReadLine());
//使用if语句根据操作决定调用哪个函数,以及无效值的错误消息
如果(操作==“+”)
{
控制台写入