Setelah saya menambahkan kredit saya ingin kembali untuk menampilkan menu utama lagi. Apa cara untuk melakukan ini? Dan apakah ada cara yang lebih mudah? Saya tidak terlalu berpengalaman dalam pemrograman.

            Console.WriteLine("-------------------------------");
            Console.WriteLine("");
            Console.WriteLine("  VENDING MACHINIES LTD");
            Console.WriteLine("");
            Console.WriteLine("-------------------------------");

            Console.WriteLine("          Main Menu");
            Console.WriteLine("");
            Console.WriteLine("1 - Add Credits (current credits= 0.00)");
            Console.WriteLine("2 - Select product/s");
            Console.WriteLine("3 - Exit");
            Cons
1
johnowen 10 Januari 2021, 21:59

3 jawaban

Jawaban Terbaik

Coba kode ini:

var  returnBack = true;
 do
{      
           Console.WriteLine("-------------------------------");
            Console.WriteLine("");
            Console.WriteLine("  VENDING MACHINIES LTD");
            Console.WriteLine("");
            Console.WriteLine("-------------------------------");

            Console.WriteLine("          Main Menu");
            Console.WriteLine("");
            Console.WriteLine("1 - Add Credits (current credits= 0.00)");
            Console.WriteLine("2 - Select product/s");
            Console.WriteLine("3 - Exit");
            Console.WriteLine("");
            Console.Write("Please Enter A Number: ");
            choice = Convert.ToInt32(Console.ReadLine());

            while (choice < 1 || choice > 3)
            {
                Console.Write("Incorrect option. Please Re-Enter: ");
                choice = Convert.ToInt32(Console.ReadLine());
            }


            switch (choice)
            {
                case 1:
                    Console.Write("How Many Credits Would You Like To Add? ");
                    credits = Convert.ToInt32(Console.ReadLine());

                    Console.Write("You Now Have {0} Credits", credits);
                    Console.WriteLine("");
                    Console.Write("Press return to go back to the main menu");
                    Console.ReadLine();
                    break;

                case 2:
                    Console.WriteLine();
                    break;
                case 3:

                returnBack=false;
                break;
               
            }
} while (returnBack);
1
Serge 10 Januari 2021, 19:19

Anda dapat menggunakan while(true) untuk siklus keabadian menu dan kembali untuk keluar dari aplikasi Anda

Console.WriteLine("-------------------------------");
Console.WriteLine("");
Console.WriteLine("  VENDING MACHINIES LTD");
Console.WriteLine("");
Console.WriteLine("-------------------------------");

while(true)
{
    Console.WriteLine("          Main Menu");
    Console.WriteLine("");
    Console.WriteLine("1 - Add Credits (current credits= 0.00)");
    Console.WriteLine("2 - Select product/s");
    Console.WriteLine("3 - Exit");
    Console.WriteLine("");
    Console.Write("Please Enter A Number: ");
    choice = Convert.ToInt32(Console.ReadLine());

    while (choice < 1 || choice > 3)
    {
        Console.Write("Incorrect option. Please Re-Enter: ");
        choice = Convert.ToInt32(Console.ReadLine());
    }


    switch (choice)
    {
        case 1:
            Console.Write("How Many Credits Would You Like To Add? ");
            credits = Convert.ToInt32(Console.ReadLine());

            Console.Write("You Now Have {0} Credits", credits);
            Console.WriteLine("");
            Console.Write("Press return to go back to the main menu");
            Console.ReadLine();
            break;

        case 2:
            Console.WriteLine();

            break;
            
        case 3:
            Console.WriteLine("Bye-bye");

            return;
    }
}
1
Lana 10 Januari 2021, 19:07

Anda dapat menggunakan metode rekursif dan saya juga menyarankan untuk mengekstrak baris tulis konsol "menu". Silahkan simak berikut ini:

static void Main(string[] args)
        {
            DisplayMenu();
            var choice = Convert.ToInt32(Console.ReadLine());
            // Final response
            choice = DisplayMenu(choice);
        }
        public static void DisplayMenu()
        {
            Console.WriteLine("          Main Menu");
            Console.WriteLine("");
            Console.WriteLine("1 - Add Credits (current credits= 0.00)");
            Console.WriteLine("2 - Select product/s");
            Console.WriteLine("3 - Exit");
            Console.WriteLine("");
            Console.Write("Please Enter A Number: ");
        }
        // Recursive overload 
        public static int DisplayMenu(int choice)
        {
            // If invalid input
            if (choice<1 || choice>3)
            {
                Console.Write("Incorrect option. Please Re-Enter: ");
                Console.WriteLine();
                DisplayMenu();
                return DisplayMenu(Convert.ToInt32(Console.ReadLine()));
            }
            // If valid show one more time the menu
            DisplayMenu();
            return Convert.ToInt32(Console.ReadLine());
        }
0
Alexandros T 10 Januari 2021, 20:00