C# 从菜单上做决定
这是我的第一篇文章,也是我在C的第一个学期。我有一个家庭作业,我已经做了好几天了,我想不出来。我会尽力解释清楚的 所以我必须创建一个类来调用另外两个类,并编译这些类来打印。假设用户从菜单中选择一个数字,该数字进行数学运算并打印答案。我无法获取生成选择并执行数学运算的代码 这是我的第一节课C# 从菜单上做决定,c#,C#,这是我的第一篇文章,也是我在C的第一个学期。我有一个家庭作业,我已经做了好几天了,我想不出来。我会尽力解释清楚的 所以我必须创建一个类来调用另外两个类,并编译这些类来打印。假设用户从菜单中选择一个数字,该数字进行数学运算并打印答案。我无法获取生成选择并执行数学运算的代码 这是我的第一节课 class MainModule { static void Main() { string assignment = "Assignment#3B-Math Operation
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对象声明为intance变量,而不是在MathMain模块中,设置该对象的第一个操作数和第二个操作数,并调用ProcessMenu。在“进程”菜单中,不要使用myNumber,而是使用声明为实例变量MathOperations的对象,并调用其相应的函数add、multiply等让我知道它是否正常工作。我有一个工作版本,我会张贴它,如果你不能得到它 以下内容只能在main方法中访问,因为它是在main方法中声明的。如果它是在方法中声明的,则只能在该方法中访问它,除非将其作为参数传递给另一个方法
MathOperationUI myNumber = new MathOperationUI();
此外,您不想调用myNumber.FirstOperator,因为myNumber是MathOperationUI类型,但FirstOperator在MathOperations中,而不是在..UI中
您的MathOperationUI应该如下所示。在类MathOperationUI中声明但在任何方法之外的MathOperations对象。这意味着您可以从MathOperationUI中的任何方法访问此对象。然后,您应该使用PromptForIntegrater的用户输入设置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()));
}
现在可以从process方法访问此对象。您可以得到它的第一个和第二个操作数,并调用加法、乘法等方法
Console.WriteLine("\nWhen adding the number {0} and {1}, the answer is {2}", usersMathOperations.FirstOperand, usersMathOperations.SecondOperand, usersMathOperations.Add());
最后,这里是正在工作的部分
希望这能让事情更清楚一点 myNumber在当前上下文中不存在,因为它应该存在于ProcessMenu中,或者它的函数应该存在于全局/实例上下文中。你在正确的轨道上,但你错过了一些分数。在MathOperationsUI类中将MathOperations对象声明为intance变量,而不是在MathMain模块中,设置该对象的第一个操作数和第二个操作数,并调用ProcessMenu。在“进程”菜单中,不要使用myNumber,而是使用声明为实例变量MathOperations的对象,并调用其相应的函数add、multiply等让我知道它是否正常工作。我有一个工作版本,我会张贴它,如果你不能得到它 以下内容只能在main方法中访问,因为它是在main方法中声明的。如果它是在方法中声明的,则只能在该方法中访问它,除非将其作为参数传递给另一个方法
MathOperationUI myNumber = new MathOperationUI();
此外,您不想调用myNumber.FirstOperator,因为myNumber是MathOperationUI类型,但FirstOperator在MathOperations中,而不是在..UI中
您的MathOperationUI应该如下所示。在类MathOperationUI中声明但在任何方法之外的MathOperations对象。这意味着您可以从MathOperationUI中的任何方法访问此对象。然后,您应该使用PromptForIntegrater的用户输入设置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()));
}
现在可以从process方法访问此对象。您可以得到它的第一个和第二个操作数,并调用加法、乘法等方法
Console.WriteLine("\nWhen adding the number {0} and {1}, the answer is {2}", usersMathOperations.FirstOperand, usersMathOperations.SecondOperand, usersMathOperations.Add());
最后,这里是正在工作的部分
希望这能让事情更清楚一点 我认为您在实现编程的几个方面遇到了问题: 范围- 价值分配- 作用域尤其重要,它几乎会影响您编写的每一行代码 也许你可以让你的导师再帮你复习一下这些概念
这并不完全是一个解决方案,因为我不愿意简单地给你家庭作业的答案。但它希望为您指明正确的方向。我认为您在实现编程的几个方面遇到了问题: 范围- 价值分配- 作用域尤其重要,它几乎会影响您编写的每一行代码 也许你可以让你的导师再帮你复习一下这些概念 这并不完全是一个解决方案,因为我不愿意简单地给你家庭作业的答案。但它有望为您指明正确的方向。看起来您刚刚退出c
当你遇到麻烦的时候,你就开始抱怨。您需要从ProcessMenu方法中删除static才能继续。我仍在努力。我去掉了静电,什么也没变。我收到一个错误,名称“myNumber”在当前上下文中不存在。看起来您遇到麻烦时刚退出编码。您需要从ProcessMenu方法中删除static才能继续。我仍在努力。我去掉了静电,什么也没变。我收到一个错误,当前上下文中不存在名称“myNumber”。