Skip to main content
 首页 » 编程设计

c#之从菜单中做出决定

2024年02月06日14qlqwjy

这是我的第一篇文章,也是我在 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。在 Process 菜单中,而不是使用 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())); 
    } 

现在您可以从 process 方法访问此对象。并且您可以获得它的第一个和第二个操作数并调用 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

希望这能让事情更清楚一些。