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

时间:2014-09-27 04:37:38

标签: c#

所以我不确定我到底做错了什么。我想我可能正在使用错误的功能。当我运行该程序时,它不会询问用户他们想要做什么,但是如果我按下按钮,它将一次只能通过一次按钮重复登录菜单。我的国旗也可能有问题。对不起,我对此很新。

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.");

    }
}

1 个答案:

答案 0 :(得分:0)

  

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

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

  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;
            }