将文件名变量传递给使用方法

时间:2015-09-13 05:07:52

标签: c# methods menu console-application

我试图根据int的选择将文件传递给方法,并通过案例转换结果并在StreamReader中使用fileName,使其成为动态而不是常量,允许用户更改。 / p>

这是我到目前为止所拥有的, 菜单:

 public static void DrawHeading()
    {
        DrawBlankLine();
        DrawLine();
        DrawBlankLine();
        Console.WriteLine("*  Poker Hand Evaluation  *");
        DrawLine();
    }

    public static void MenuContent(int maxItems)
    {
        DrawBlankLine();
        Console.WriteLine(" 1.  File: Flush.txt");
        Console.WriteLine(" 2.  File: FourKind.txt");
        Console.WriteLine(" 3.  File: FullHouse.txt");
        Console.WriteLine(" 4.  File: Pair.txt");
        Console.WriteLine(" 5.  File: RoyalFlush.txt");
        Console.WriteLine(" 6.  File: Straight.txt");
        Console.WriteLine(" 7.  File: StraightFlush.txt");
        Console.WriteLine(" 8.  File: ThreeKind.txt");
        Console.WriteLine(" 9.  File: TwoPair.txt");
        Console.WriteLine(" 10. Enter file name");
        Console.WriteLine(" {0}. Exit program", maxItems);
        DrawBlankLine();
        Console.WriteLine("Make your choice: "
        + "Please type 1, 2,... or {0} for exit.", maxItems);
        DrawBlankLine();
        DrawLine();
        DrawBlankLine();
    }

    public static void DisplayMenu()
    {
        const int MAX_MENU_ITEMS = 11;

        bool maxMenu = false;
        int getMenuSelection = 0;
        string fileName = null;

        while (getMenuSelection != MAX_MENU_ITEMS)
        {
            Console.Clear();
            DrawHeading();
            MenuContent(MAX_MENU_ITEMS);

            maxMenu = int.TryParse(Console.ReadLine(), out getMenuSelection);

            if (maxMenu)
            {
                switch (getMenuSelection)
                {
                    case 1:
                        fileName = "RoyalFlush.txt";
                        Console.WriteLine("File Name: {0}", fileName);
                        break;
                    case 2:
                        fileName = "StraightFlush.txt";
                        break;
                    case 3:
                        fileName = "FourKind.txt";
                        break;
                    case 4:
                        fileName = "FullHouse.txt";
                        break;
                    case 5:
                        fileName = "Flush.txt";
                        break;
                    case 6:
                        fileName = "Straight.txt";
                        break;
                    case 7:
                        fileName = "ThreeKind.txt"; ;
                        break;
                    case 8:
                        fileName = "TwoPair.txt"; ;
                        break;
                    case 9:
                        fileName = "Pair.txt"; ;
                        break;
                    case 10:
                        Case10();
                        break;
                    case 11:
                        Console.WriteLine("Exiting program...");
                        System.Environment.Exit(0);
                        break;
                    default:
                        if (getMenuSelection != MAX_MENU_ITEMS)
                        {
                            MenuErrorMessage();
                        }
                        break;
                }
                //return fileName;
            }
            else
            {
                MenuErrorMessage();
            }
            Console.ReadLine();
        }
        //return fileName;
    }

使用fileName时的方法:

    public static void LoadHand(Card[] data)
    {
        string fileName = null;

        Console.WriteLine("file name: {0}", fileName);
        Console.ReadLine();
        string input = fileName;

        List<Card> cards = new List<Card>();

        StreamReader inFile = new StreamReader(fileName);//open file

void LoadHand的代码

这就是我所拥有的:

    public static void LoadHand(Card[] data)
    {
        string fileName = null;

        Console.WriteLine("file name: {0}", fileName);
        Console.ReadLine();
        string input = fileName;

        List<Card> cards = new List<Card>();

        StreamReader inFile = new StreamReader(fileName);//open file
        input = inFile.ReadLine();//priming read for array

        string[] inputArray = input.Split(new char[] { ' ' },
            StringSplitOptions.RemoveEmptyEntries);

        //input loading file content into array of structures

        for (int i = 0; i < data.Length; ++i)
        {
            Card newCard = new Card();
            newCard.suit = inputArray[i * 2][0];
            newCard.value = int.Parse(inputArray[i * 2 + 1]);
            data[i] = newCard;
        }
        inFile.Close();//Close file
    }'

1 个答案:

答案 0 :(得分:0)

  • DisplayMenu的签名更改为static string DisplayMenu()
  • 删除return fileName;DisplayMenu上的评论(//)。
  • 将fileName作为参数添加到LoadHand

    public static void LoadHand(Card[] data, string path)
    {
      List<Card> cards = new List<Card>();
      StreamReader inFile = new StreamReader(path);//open file