簡體   English   中英

將用戶輸入存儲到 C# 中的並行數組

[英]Store the user input to parallel arrayin C#

我試圖給用戶 2 個選項,一個是查看數組存儲的內容,第二個選項是輸入新項目。 請提供任何幫助,我嘗試在 ItemInfo() 方法中添加數組,但效果不佳:我嘗試在 switch 中這樣做:

ItemInfo(itemNArr[itemN],itemNameArr[itemName],itemPriceArr[itemPrice],itemStockArr[itemNStock],itemRatingArr[itemRating] 

但仍然無法正常工作。 那么在這種情況下我應該怎么做才能將用戶輸入傳遞給數組並存儲它呢? 我將不勝感激。

我遇到的問題是:

The name 'itemN' does not exist in the current context line 37
The name 'itemName' does not exist in the current context line 37
The name 'itemPrice' does not exist in the current context line 37
The name 'itemNStock' does not exist in the current context line 37
The name 'itemRating' does not exist in the current context line 37

代碼在這里:

using System;
using System.Security.Cryptography.X509Certificates;
using static System.Console;
namespace program
    {
        class UseItem
        {
        public static void Main(string[] args)
        {

            int item = AppIntro();
            string[] itemNArr = new string[item];
            string[] itemNameArr = new string[item];
            double[] itemPriceArr = new double[item];
            int[] itemStockArr = new int[item];
            int[] itemRateArr = new int[item];
            // ask to enter price for produect
            for (int i = 0; i < item; i++)
            {
                Clear();
                ItemInfo(i,  out itemNameArr[i], out itemPriceArr[i],  itemStockArr[i],  itemRateArr[i]);
                Clear();
            }
            string ans;
            do
            {
                WriteLine("What would you like to do next?");
                WriteLine(" Enter 1 to display individual course" + " and 2 to add a new product");
                int option = int.Parse(ReadLine());
                int result = int.Parse(ReadLine());
                switch (option)
                {
                    case 1:
                        DisplayItems(itemNArr,itemNameArr,itemPriceArr, itemStockArr, itemRateArr);
                        break;
                    case 2:
                        result = ItemInfo(itemN, itemName,itemPrice,itemNStock,itemRating);
                        break;
                    default:
                        WriteLine("No valid entery was entered. " + "i decided to exit the application.... ");
                        break;
                }
                WriteLine("\n\nWould you like to do another operation?");
                ans = ReadLine();
            } while (ans == "Yes" || ans == "yes");
            WriteLine("\n\nThank you for choosing our application.... coma back again :) ");
        }
            public static int AppIntro()
            {
            WriteLine("Welcome to the PSO App: ");
            WriteLine("You will be asked to enter the product name" + " product price, how many you have in stock of the product" + " and the rate of the product");
            WriteLine("Then you will have a choise to display individual product" + "info");
            WriteLine("\n\nHow many products you want to add!?");
                return int.Parse(ReadLine());
            }
        public static void ItemInfo(int itemN, out string itemName, out double itemPrice, int itemNStock, int itemRating)
        {
            Write(" Enter the item number {0}:", itemN+1);
            itemName = ReadLine();
            Write(" Enter the item name: ");
            itemName = ReadLine();
            Write(" Enter the item price: ");
            itemPrice = double.Parse(ReadLine());
            Write(" Enter the number of the item in stock: ");
            itemNStock = int.Parse(ReadLine());
            Write(" Enter the rate of the item: ");
            itemRating = int.Parse(ReadLine());
        }
        public static void DisplayItems(string[]itemN, string[] itemName, double[] itemPrice, int[] itemNStock, int[] itemRating)
        {
            Write(" Which items would you like to display? Enter it's number: ");
            string valueIn = ReadLine();

            int n = 0;
            for(int i=0; i < itemN.Length; i++)
            {
                if(valueIn == itemN[i])
                {
                    n = i;
                }
                Clear();
                WriteLine("Your item info: ");
                WriteLine("item number is: " + itemN[n]);
                WriteLine("item name is: " + itemName[n]);
                WriteLine(" item price is: " + itemPrice[n]);
                WriteLine(" number of item in the stock is: " + itemNStock[n]);
                WriteLine(" item rate is: " + itemRating[n]);
            }
        }
            }

        }

我已經接近答案了,最后我想說這是我問題的答案。

using System;
using System.Security.Cryptography.X509Certificates;
using static System.Console;
namespace program
    {
        class UseItem
        {
        public static void Main(string[] args)
        {
            int n = 0;
            int item = AppIntro();
           
            string[] itemNArr = new string[item];
            string[] itemNameArr = new string[item];
            double[] itemPriceArr = new double[item];
            int[] itemStockArr = new int[item];
            int[] itemRateArr = new int[item];
            // ask to enter price for produect
            //for (int i = 0; i < item; i++)
            //{
            //Clear();
            n = ItemInfo(n, itemNArr, itemNameArr, itemPriceArr, itemStockArr, itemRateArr);
            //Clear();
            //}
            string ans;
            do
            {
               // WriteLine("What would you like to do next?");
                WriteLine(" Enter 1 to display individual course" + " and 2 to add a new product");
                int option = int.Parse(ReadLine());
                //int result = int.Parse(ReadLine());
                
                switch (option)
                {
                    case 1:
                        DisplayItems(itemNArr,itemNameArr,itemPriceArr, itemStockArr, itemRateArr);
                        break;
                    case 2:
                        n = ItemInfo(n, itemNArr, itemNameArr, itemPriceArr, itemStockArr, itemRateArr);
                        break;
                    default:
                        WriteLine("No valid entery was entered. " + "i decided to exit the application.... ");
                        break;
                }
                WriteLine("\n\nWould you like to do another operation?");
                ans = ReadLine();
            } while (ans == "Yes" || ans == "yes");
            WriteLine("\n\nThank you for choosing our application.... come back again :) ");
        }
            public static int AppIntro()
            {
            WriteLine("Welcome to the PSO App: ");
            WriteLine("You will be asked to enter the product name" + " product price, how many you have in stock of the product" + " and the rate of the product");
            WriteLine("Then you will have a choise to display individual product" + "info");
            WriteLine("\n\nHow many products you want to add!?");
            return int.Parse(Console.ReadLine());
            }
        public static int ItemInfo(int n, string[] itemN, string[] itemName, double[] itemPrice, int[] itemNStock, int[] itemRating)
        {


                Write(" Enter the item number:");
            itemN[n] = ReadLine();
                Write(" Enter the item name: ");
            itemName[n] = ReadLine();

            Write(" Enter the item price: ");
            itemPrice[n] = double.Parse(ReadLine());
                Write(" Enter the number of the item in stock: ");
            itemNStock[n] = int.Parse(ReadLine());
                Write(" Enter the rate of the item: ");
            itemRating[n] = int.Parse(ReadLine());
            n++;
            return n;
        }
        public static void DisplayItems(string[]itemN, string[] itemName, double[] itemPrice, int[] itemNStock, int[] itemRating)
        {
            Write(" Which items would you like to display? Enter it's number: ");
            string valueIn = ReadLine();

            int n = 0;
            for(int i=0; i < itemN.Length; i++)
            {
                if(valueIn == itemN[i])
                {
                    n = i;
                }
                Clear();
                WriteLine("Your item info: ");
                WriteLine("item number is: " + itemN[n]);
                WriteLine("item name is: " + itemName[n]);
                WriteLine(" item price is: " + itemPrice[n]);
                WriteLine(" number of item in the stock is: " + itemNStock[n]);
                WriteLine(" item rate is: " + itemRating[n]);
            }
        }
            }

        }
    

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM