开关无法正常工作,控制台上没有显示所需的数据

问题描述 投票:0回答:1

-> 当我按 case 1 并插入值时,控制台不会显示作者姓名。 enter image description here enter image description here -> 情况 2 在我插入作者姓名后,控制台不显示作者在数组中的索引和位置。 enter image description here enter image description here

using System;

namespace BookShelf
{
    internal class Program
    {
        static void Main(string[] args)
        {
            bool isOpen = true;
            string[,] books =
            {
               {"William Shakespeare","Jane Austen","Charles Dickens" },
               {"Mark Twain","Leo Tolstoy","George Orwell" },
               {"Virginia Woolf","Gabriel García Márquez","F. Scott Fitzgerald" }
            };

            while (isOpen)
            { 
                Console.SetCursorPosition(0, 20);
                Console.WriteLine("Author list:\n");
                for(int i = 0; i < books.GetLength(0); i++)
                {
                    for(int j = 0; j < books.GetLength(1); j++)
                    {
                        Console.Write(books[i, j] + " | ");
                    }
                    Console.WriteLine();
                }
                Console.SetCursorPosition(0, 0);
                Console.WriteLine("Libray");
                Console.WriteLine("\n1. Finde author by book index. " +
                    "\n\n2. Finde book by auhor index. \n\n3. Exit");
                Console.Write("Choose menu point:");
                switch(Convert.ToInt32(Console.ReadLine()))
                {
                    case 1:
                        int line, column;
                        Console.WriteLine("tell number of the shelf");
                        line = Convert.ToInt32(Console.ReadLine())-1;
                        Console.WriteLine("tell number of the column");
                        column = Convert.ToInt32(Console.ReadLine())-1;
                        Console.WriteLine("Author name: "+books[line,column]);
                        break;

                    case 2:
                        string author;
                        bool authorIsFound = false;
                        Console.WriteLine("Insert author: ");
                        author= Console.ReadLine();
                        for(int i = 0;i < books.GetLength(0);i++)
                        {
                            for(int j = 0;j < books.GetLength(1); j++)
                            {
                                if (author.ToLower() == books[i, j].ToLower())
                                {
                                    Console.WriteLine($"Author{books[i,j]}is on index shelf{i+1} place{j+1}.");
                                    authorIsFound = true;
                                }
                            }
                        }
                        if (authorIsFound == false)
                        {
                            Console.WriteLine("This author doesnt exist");
                        }
                        break;

                    case 3:
                        isOpen = false;
                        break;

                    default:
                        Console.WriteLine("Wrong instruction");
                        break;
                }
                if (isOpen)
                {
                    Console.WriteLine("\nPress any key to continue");
                       
                }
                Console.Clear();
                Console.WriteLine();
                Console.ReadKey();
            }
        }
    }
}

-> 情况 1 期望插入数组(架子)的行和列并获取作者 -> 案例 2 期望插入作者姓名并获取索引和数组(架子)的位置

c# arrays multidimensional-array
1个回答
0
投票

你可以尝试下面的代码吗? 据我了解,您对案例 1 和案例 2 有问题。

using System;

namespace BookShelf
{
    internal class Program
    {
        static void Main(string[] args)
        {
            bool isOpen = true;
            string[,] books = {
                {"William Shakespeare", "Jane Austen", "Charles Dickens"},
                {"Mark Twain", "Leo Tolstoy", "George Orwell"},
                {"Virginia Woolf", "Gabriel García Márquez", "F. Scott Fitzgerald"}
            };

            while (isOpen)
            {
                Console.SetCursorPosition(0, 20);
                Console.WriteLine("Author list:\n");

                for (int i = 0; i < books.GetLength(0); i++)
                {
                    for (int j = 0; j < books.GetLength(1); j++)
                    {
                        Console.Write(books[i, j] + " | ");
                    }
                    Console.WriteLine();
                }

                Console.SetCursorPosition(0, 0);
                Console.WriteLine("Library");
                Console.WriteLine("\n1. Find author by book index.");
                Console.WriteLine("2. Find book by author index.");
                Console.WriteLine("3. Exit");
                Console.Write("Choose menu point:");

                switch (Convert.ToInt32(Console.ReadLine()))
                {
                    case 1:
                        int line, column;
                        Console.WriteLine("Enter the shelf number:");
                        line = Convert.ToInt32(Console.ReadLine()) - 1;
                        Console.WriteLine("Enter the column number:");
                        column = Convert.ToInt32(Console.ReadLine()) - 1;

                        if (line >= 0 && line < books.GetLength(0) && column >= 0 && column < books.GetLength(1))
                        {
                            Console.WriteLine("Author name: " + books[line, column]);
                        }
                        else
                        {
                            Console.WriteLine("Invalid shelf or column number.");
                        }
                        break;

                    case 2:
                        string author;
                        bool authorIsFound = false;
                        Console.WriteLine("Insert author: ");
                        author = Console.ReadLine();

                        for (int i = 0; i < books.GetLength(0); i++)
                        {
                            for (int j = 0; j < books.GetLength(1); j++)
                            {
                                if (author.ToLower() == books[i, j].ToLower())
                                {
                                    Console.WriteLine($"Author {books[i, j]} is on shelf {i + 1}, place {j + 1}.");
                                    authorIsFound = true;
                                    break;  // Exit the inner loop once the author is found.
                                }
                            }
                            if (authorIsFound)
                            {
                                break;  // Exit the outer loop once the author is found.
                            }
                        }

                        if (!authorIsFound)
                        {
                            Console.WriteLine("This author doesn't exist in the library.");
                        }
                        break;

                    case 3:
                        isOpen = false;
                        break;

                    default:
                        Console.WriteLine("Wrong instruction");
                        break;
                }

                if (isOpen)
                {
                    Console.WriteLine("\nPress any key to continue");
                }

                Console.Clear();
                Console.WriteLine();
                Console.ReadKey();
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.