0

我有一个明天到期的项目,但我遇到了麻烦。我是编程新手,我正在使用并行数组为学校构建一个项目。当我AddItem从我的 main 访问我的方法时,它每次都会覆盖以前的方法调用。如果您对我如何解决此问题有任何意见,请告诉我。以下是有问题的两种方法:

using System;
using System.Linq;  
using System.IO;

namespace Assignment3
{
    class Program
    {
        static void Main()
        {
            // set the physical array size and declare the parallel arrays
            const int PHYSICAL_ARRAY_SIZE = 6;
            string[] menuItems = new string[PHYSICAL_ARRAY_SIZE];
            double[] menuPrices = new double[PHYSICAL_ARRAY_SIZE];

            // set the menu choices and quiz parameters
            char[] menuChoices = { '1', '2', '3', '4', '5', '6', '7', '0' };
            char userChoice;
            int logicalArraySize = 0;
            do
            {              
                userChoice = GetMenuChoice(menuChoices);

                if (userChoice == '1')
                {
                    logicalArraySize = AddItem(menuItems, menuPrices);
                }
                else 
                {
                    ProcessMenuChoice(userChoice, menuPrices, menuItems,
                        ref logicalArraySize, PHYSICAL_ARRAY_SIZE);
                }
            } while (userChoice != menuChoices[^1]);
        }


        public static int AddItem(string[] items, double[] prices) 
        {
            double price;
            string description;
            int count = 0;

            Console.Clear();
            Console.WriteLine("New Item");
            Console.WriteLine("-----------------------------------------");
            Console.WriteLine("Enter an item to add to bill: ");
            
            string message = "Enter description: ";
            description = PromptForStringNotEmpty(message);

            Console.Write($"Enter price of {description}: ");
            price = GetSafeDouble();

            Console.WriteLine("Add Item was successful");
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
            Console.Clear();

            prices[count] = price; 
            items[count] = description; 
            count++;
    
            return count;
        }
4

0 回答 0