这是我的第一篇文章,也是我在C#中的第一学期。我已经从事了几天的家庭作业,但无法弄清楚。我将尽力解释。
因此,我必须创建一个类来调用其他两个类并编译要打印的类。用户应该从菜单中选择一个数字,并且该数字应该进行数学运算并打印答案。我无法获得生成选择并执行数学运算的代码。
这是我的第一堂课。
class MainModule
{
static void Main()
{
string assignment = "Assignment#3B-Math Operations Modified";
MathOperationUI myNumber = new MathOperationUI();
myNumber.MathMainModule();
Console.ReadLine();
这是我的第二堂课。
class MathOperations
{
int firstOperand;
int secondOperand;
public int FirstOperand
{
get
{
return firstOperand;
}
set
{
firstOperand = value;
}
}
public int SecondOperand
{
get
{
return secondOperand;
}
set
{
secondOperand = value;
}
}
public MathOperations()
{
firstOperand = 0;
secondOperand = 0;
}
public double Add()
{
double theAddition;
theAddition = (firstOperand + secondOperand);
return theAddition;
}
public double Subtract()
{
double theSubtraction;
theSubtraction = (firstOperand - secondOperand);
return theSubtraction;
}
public double Multiply()
{
double theMultiplication;
theMultiplication = (firstOperand * secondOperand);
return theMultiplication;
}
public double Divide()
{
double theDivision;
theDivision = (float)firstOperand / (float)secondOperand;
return theDivision;
而我的最后一堂课给了我这个问题。
class MathOperationUI
{
public MathOperationUI()
{
}
public void MathMainModule()
{
int firstOperand;
int secondOperand;
DisplayMenu();
MathOperations usersMathOperations;
firstOperand = PromptForInterger("first");
secondOperand = PromptForInterger("second");
usersMathOperations = new MathOperations ();
}
public void DisplayMenu()
{
Console.WriteLine("\n\tMenu");
Console.WriteLine("****************************");
Console.WriteLine("1: Addition Operation");
Console.WriteLine("2: Subtraction Operation");
Console.WriteLine("3: Multiplication Operation");
Console.WriteLine("4: Division Operation");
Console.WriteLine("5: Exit");
Console.WriteLine("****************************");
}
static int ProcessMenu(int choice)
{
if (choice == 1)
Console.WriteLine("\nWhen adding the number {0} and {1}, the answer is {2}", myNumber.FirstOperand, myNumber.SecondOperand, addition);
else
if (choice == 2)
Console.WriteLine("\nWhen subtracting the number {0} and {1}, the answer is {2}", myNumber.FirstOperand, myNumber.SecondOperand, subtraction);
else
if (choice == 3)
Console.WriteLine("\nWhen multipling the number {0} and {1}, the answer is {2}", myNumber.FirstOperand, myNumber.SecondOperand, multiplication);
else
if (choice == 4)
Console.WriteLine("\nWhen dividing the number {0} and {1}, the answer is {2:F2}", myNumber.FirstOperand, myNumber.SecondOperand, division);
else
if (choice == 5)
return 0;
}
static int PromptForInterger(string position)
{
Console.WriteLine("\n\nEnter the {0} number:\t", position);
return (int.Parse(Console.ReadLine()));
}
最佳答案
myNumber在当前上下文中不存在,因为它应该存在于ProcessMenu函数中,或者应该存在于全局/实例上下文中。您处在正确的轨道上,但缺少一些要点。在MathOperationsUI类中声明一个MathOperations对象作为实例变量,而不是在MathMainModule中设置该对象的FirstOperand和SecondOperand并调用ProcessMenu。在“处理”菜单中,而不是使用myNumber,请使用您声明为实例变量的对象(MathOperations),并调用其适当的函数(加,乘等),让我知道它是否起作用。我有一个工作版本,如果无法获取,我会发布。
只能在main方法中访问以下内容,因为它是在此处声明的。如果在方法中声明了它,则只能在该方法中访问它,除非您将其作为参数传递给另一个方法。
MathOperationUI myNumber = new MathOperationUI();
此外,您不希望调用
myNumber.FirstOperand
,因为myNumber是MathOperationUI类型,但是FirstOperand在MathOperations中而不在.UI中。您的MathOperationUI应该如下所示。在类(MathOperationUI)中声明但在任何方法之外的MathOperations对象。这意味着您可以从MathOperationUI中的任何方法访问此对象。然后,应使用PromptForInterger的用户输入设置MathOperations(第一和第二操作数)的属性。最后,您应该调用ProcessMenu方法来处理这些输入。
public class MathOperationUI
{
MathOperations usersMathOperations;
public MathOperationUI()
{
usersMathOperations = new MathOperations();
}
public void MathMainModule()
{
DisplayMenu();
usersMathOperations.FirstOperand = PromptForInterger("first");
usersMathOperations.SecondOperand = PromptForInterger("second");
Console.WriteLine("\n\nEnter your coice");
ProcessMenu(int.Parse(Console.ReadLine()));
}
现在,您可以从处理方法访问该对象。您可以获取其第一个和第二个操作数,并调用Add,Multiply等方法。
Console.WriteLine("\nWhen adding the number {0} and {1}, the answer is {2}", usersMathOperations.FirstOperand, usersMathOperations.SecondOperand, usersMathOperations.Add());
最后,这是工作的作品https://dotnetfiddle.net/5lE63L
希望这可以使事情变得更清晰。