我认为我的功能可能是错误的

本文关键字:错误 功能 我的 | 更新日期: 2023-09-27 18:24:48

所以我不确定我到底做错了什么。我想我可能用错了函数。当我运行程序时,它不会询问用户想做什么,但如果我按下一个按钮,它会不断地将菜单记录到控制台,只需一次按下。我的国旗也可能有问题。对不起,我对此很陌生。

using System;
class Purse
{
    double check_balance(int p, int n, int d, int q)
    {
        return (p + 5 * n + 10 * d + 25 * q);
    }
    double insert(int p, int n, int d, int q)
    {
        return (p + 5 * n + 10 * d + 25 * q);
    }
    double remove(int p, int n, int d, int q)
    {
        return (p + 5 * n + 10 * d + 25 * q);
    }
    public static void Main()
    {
        int p = 0, n = 0, d = 0, q = 0, pChange = 0, nChange = 0, dChange = 0, qChange = 0;
        bool flag = true; // Flag
        char menu;
        Purse myBalance = new Purse(); 
        menu = Console.ReadKey().KeyChar;
        while (flag)
        {
            Console.WriteLine("What would you like to do?'n1. Check Balance'n2. Deposit'n3. Withdraw'n0. Close program");
            if (menu == 1)// Check the balance
            {
                myBalance.check_balance(p, n, d, q);
                continue;
            }
            else if (menu == 2)// Deposit
            {
                Console.WriteLine("Deposit how many pennies?");
                pChange = int.Parse(Console.ReadLine());
                p = p + pChange;
                Console.WriteLine("Deposit how many nickels?");
                nChange = int.Parse(Console.ReadLine());
                n = n + nChange;
                Console.WriteLine("Deposit how many dimes?");
                dChange = int.Parse(Console.ReadLine());
                d = d + dChange;
                Console.WriteLine("Deposit how many quarters?");
                qChange = int.Parse(Console.ReadLine());
                q = q + qChange;
                myBalance.insert(p, n, d, q);
                continue;
            }
            else if (menu == 3)// Withdraw
            {
                Console.WriteLine("Withdraw how many pennies?");
                pChange = int.Parse(Console.ReadLine());
                p = p - pChange;
                Console.WriteLine("Withdraw how many nickels?");
                nChange = int.Parse(Console.ReadLine());
                n = n - nChange;
                Console.WriteLine("withdraw how many dimes?");
                dChange = int.Parse(Console.ReadLine());
                d = d - dChange;
                Console.WriteLine("Withdraw how many quarters?");
                qChange = int.Parse(Console.ReadLine());
                q = q - qChange;
                myBalance.remove(p, n, d, q);
                continue;
            }
            else if(menu == 0)// close the menu
            {
                flag = false;
                break;
            }
            else// Improper input
            {
                Console.WriteLine("Please enter a menu number.");
                continue;
            }
        }
        Console.WriteLine("The program will now exit.");
    }
}

我认为我的功能可能是错误的

当我运行程序时,它不会询问用户他们想做什么

只需在Console.WriteLine("What would you like to do?'n1. Check Balance'n2. Deposit'n3. Withdraw'n0. Close program");之后移动menu = Console.ReadKey().KeyChar;,它应该像:

  while (flag)
                {
                    Console.WriteLine(
                        "What would you like to do?'n1. Check Balance'n2. Deposit'n3. Withdraw'n0. Close program");
                    menu = Console.ReadKey().KeyChar;

如果我按下一个按钮,只需一次按下,它就会不断地将菜单记录到控制台。

因为

1-Console.ReadKey()不在主循环中。2-您应该将menu的值作为类似于if(menu=='0')的字符或其值if(menu==48)进行比较。

例如

            else if (menu == 0) // close the menu
            {
                flag = false;
                break;
            }

应该改为

            else if (menu == '0') // close the menu
            {
                flag = false;
                break;
            }