You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Visual Studio 2022控制台应用switch语句最后分支无显示问题求助

问题与解决方案

问题说明

我是平台新手,不太熟悉使用规则。我的Visual Studio 2022控制台应用中,switch语句的最后分支无法在控制台显示,运行代码片段时正常,但完整运行时该分支似乎被忽略,控制台直接关闭且无报错信息。之前因不了解代码块格式导致代码排版混乱,现已修正,以下是我的代码:

namespace langlopendetaak_Test
{
    internal class Program
    {
        static void Main(string[] args)
        {
            bool admin;
            string nameShop = "KickFitSupplies";
            Console.WriteLine("Hoe heet je? ");
            string nameUser = Console.ReadLine();
            Console.ForegroundColor = ConsoleColor.Green;
            Console.BackgroundColor = ConsoleColor.White;
            Console.WriteLine($"Welkom, {nameUser} bij {nameShop}! Wij verkopen de beste" +
                $" uitrustingen voor Thaiboksen en Mua-Thai \nje kan hier bokshandschoenen, Scheenbeschermers, " +
                $"kruisbeschermers, Boksbandages en Mondstukken kopen.");
            Console.ResetColor();
            string nameProduct_1 = "Bokshandschoenen";
            string nameProduct_2 = "Scheenbeschermers";
            string nameProduct_3 = "Kruisbeschermers";
            float priceProduct_1 = 59.99f;
            float priceProduct_2 = 55.49f;
            float priceProduct_3 = 25.00f;
            int quantityOfProduct_1 = 20;
            int quantityOfProduct_2 = 20;
            int quantityOfProduct_3 = 15;
            string descriptionProduct_1 = "Rode kunstleren Bokshandschoenen";
            string descriptionProduct_2 = "Zwarte kunstleren Scheenbeschermers";
            string descriptionProduct_3 = "Witte synthetisch leer Kruisbeschermers";
            string production_info_1 = "De fabrikant voor  de bokshandschoenen is Everlast";
            string production_info_2 = "De fabrikant voor  de scheenbeschermers is Everlast";
            string production_info_3 = "De fabrikant voor  de kruisbeschermers is Venum";



            Console.Write($"Product 1: {nameProduct_1} Aantal:  {quantityOfProduct_1}" +
                $"\nProduct 2: {nameProduct_2} Aantal:  {quantityOfProduct_2}" +
                $"\nProduct 3: {nameProduct_3} Aantal:  {quantityOfProduct_3}");

            Console.Write("\n1. item x bekijken\n2. item x kopen");

            int firstUserOption = Convert.ToInt32(Console.ReadLine());
            switch (firstUserOption)
            {
                case 1:
                    Console.WriteLine("Welke product wil je bekijken? ");
                    Console.WriteLine("kies uit 1, 2, 3, 4 of 5 ");
                    int optionForProducts = Convert.ToInt32(Console.ReadLine());
                    Console.Clear();
                    switch (optionForProducts)
                    {
                        case 1:
                            Console.WriteLine($"Naam van het product: {nameProduct_1} " +
                                $"Prijs(in euros)= {priceProduct_1} " +
                                $"Aantal van het product: {quantityOfProduct_1}" +
                                $"\nBeschrijving van het product: {descriptionProduct_1}" +
                                $"\nExtra info over het product: {production_info_1}");
                            break;
                        case 2:
                            Console.WriteLine($"Naam van het product: {nameProduct_2} " +
                                $"Prijs(in euros)= {priceProduct_2} " +
                                $"Aantal van het product: {quantityOfProduct_2}" +
                                $"\nBeschrijving van het product: {descriptionProduct_2}" +
                                $"\nExtra info over het product: {production_info_2}");
                            break;
                        case 3:
                            Console.WriteLine($"Naam van het product: {nameProduct_3} " +
                                $"Prijs(in euros)= {priceProduct_3} " +
                                $"Aantal van het product: {quantityOfProduct_3}" +
                                $"\nBeschrijving van het product: {descriptionProduct_3}" +
                                $"\nExtra info over het product: {production_info_3}");
                            break;
                    }
                    break;
                case 2:
                    Console.WriteLine("Welk product wil je kopen? ");
                    int buyingOption = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("hoeveel wil je ervan kopen? ");
                    int optiequantityOfProduct = Convert.ToInt32(Console.ReadLine());

                    switch (buyingOption)
                    {
                        case 1:
                            if (optiequantityOfProduct > quantityOfProduct_1)
                            {
                                Console.ForegroundColor = ConsoleColor.Red;
                                Console.WriteLine($"Sorry, er zijn maar {quantityOfProduct_1} {nameProduct_1}" +
                                    $" in voorraad");
                                Console.ResetColor();
                            }
                            else if (optiequantityOfProduct < quantityOfProduct_1)
                            {
                                int newquantityOfProduct_1 = quantityOfProduct_1 - optiequantityOfProduct;
                                Console.ForegroundColor = ConsoleColor.Green;
                                Console.WriteLine($"Er zijn succesvol {optiequantityOfProduct} {nameProduct_1}" +
                                    $"gekocht!");
                                Console.ResetColor();
                                float totalPrice = priceProduct_1 * optiequantityOfProduct;
                                float totalPriceBTW = totalPrice + totalPrice * 0.21f;
                                Console.WriteLine($"Totale prijs= {totalPrice} Totale prijs (incl. btw) = ");
                                Console.ForegroundColor = ConsoleColor.Red;
                                Console.Write($"{totalPriceBTW}");
                                Console.ResetColor();

                            }

                            break;
                        case 2:
                            if (optiequantityOfProduct > quantityOfProduct_2)
                            {
                                Console.ForegroundColor = ConsoleColor.Red;
                                Console.WriteLine($"Sorry, er zijn maar {quantityOfProduct_2} {nameProduct_2}" +
                                    $" in voorraad");
                                Console.ResetColor();
                            }
                            else if (optiequantityOfProduct < quantityOfProduct_2)
                            {
                                int newquantityOfProduct_2 = quantityOfProduct_2 - optiequantityOfProduct;
                                Console.ForegroundColor = ConsoleColor.Green;
                                Console.WriteLine($"Er zijn succesvol {optiequantityOfProduct} {nameProduct_2}" +
                                    $"gekocht!");
                                Console.ResetColor();
                                float totalPrice = priceProduct_2 * optiequantityOfProduct;
                                float totalPriceBTW = totalPrice + totalPrice * 0.21f;
                                Console.WriteLine($"Totale prijs= {totalPrice} \n" +
                                    $"Totale prijs (incl. btw) = {totalPriceBTW}");
                            }
                            break;
                        case 3:
                            if (optiequantityOfProduct > quantityOfProduct_3)
                            {
                                Console.ForegroundColor = ConsoleColor.Red;
                                Console.WriteLine($"Sorry, er zijn maar {quantityOfProduct_3} {nameProduct_3}" +
                                    $" in voorraad");
                                Console.ResetColor();
                            }
                            else if (optiequantityOfProduct < quantityOfProduct_3)
                            {
                                int newquantityOfProduct_3 = quantityOfProduct_3 - optiequantityOfProduct;
                                Console.ForegroundColor = ConsoleColor.Green;
                                Console.WriteLine($"Er zijn succesvol {optiequantityOfProduct} {nameProduct_3}" +
                                    $"gekocht!");
                                Console.ResetColor();
                                float totalPrice = priceProduct_3 * optiequantityOfProduct;
                                float totalPriceBTW = totalPrice + totalPrice * 0.21f;
                                Console.WriteLine($"Totale prijs= {totalPrice} \n" +
                                    $"Totale prijs (incl. btw) = {totalPriceBTW}");
                            }
                            break;
                        default:
                            break;
                    }
                    break;
            }
        }
    }
}

问题原因与修复方案

1. 控制台自动关闭导致看不到输出

程序执行完Main方法后会立即退出,控制台窗口直接关闭,即使最后分支有输出也来不及查看。

修复:在Main方法的最后一行添加:

Console.ReadKey(); // 等待用户按下任意键再关闭控制台

2. Switch分支逻辑不完善

  • Default分支无输出:buyingOption的default分支为空,用户输入非1/2/3的数字时,没有任何提示就结束程序。
  • 未处理等于库存的情况:当前代码只判断了optiequantityOfProduct > 库存和optiequantityOfProduct < 库存,当输入数量等于库存时,不会进入任何分支,无输出。

修复:

  • 给default分支添加提示:
    default:
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine("Ongeldige productkeuze! Kies aub 1, 2 of 3.");
        Console.ResetColor();
        break;
    
  • 把else if (optiequantityOfProduct < 库存)改为else,覆盖等于库存的情况,或者单独添加else if (optiequantityOfProduct == 库存)的处理逻辑。例如:
    else
    {
        int newquantityOfProduct_1 = quantityOfProduct_1 - optiequantityOfProduct;
        Console.ForegroundColor = ConsoleColor.Green;
        Console.WriteLine($"Er zijn succesvol {optiequantityOfProduct} {nameProduct_1} gekocht! Nu is de voorraad leeg.");
        Console.ResetColor();
        float totalPrice = priceProduct_1 * optiequantityOfProduct;
        float totalPriceBTW = totalPrice + totalPrice * 0.21f;
        Console.WriteLine($"Totale prijs= {totalPrice} Totale prijs (incl. btw) = ");
        Console.ForegroundColor = ConsoleColor.Red;
        Console.Write($"{totalPriceBTW}");
        Console.ResetColor();
    }
    

3. 输入验证缺失

使用Convert.ToInt32时,如果用户输入非数字内容,程序会直接崩溃。建议替换为int.TryParse做输入验证:

示例:

int firstUserOption;
while (!int.TryParse(Console.ReadLine(), out firstUserOption) || firstUserOption < 1 || firstUserOption > 2)
{
    Console.WriteLine("Ongeldige invoer! Kies aub 1 of 2.");
}

修改后的完整代码示例

namespace langlopendetaak_Test
{
    internal class Program
    {
        static void Main(string[] args)
        {
            bool admin;
            string nameShop = "KickFitSupplies";
            Console.WriteLine("Hoe heet je? ");
            string nameUser = Console.ReadLine();
            Console.ForegroundColor = ConsoleColor.Green;
            Console.BackgroundColor = ConsoleColor.White;
            Console.WriteLine($"Welkom, {nameUser} bij {nameShop}! Wij verkopen de beste" +
                $" uitrustingen voor Thaiboksen en Mua-Thai \nje kan hier bokshandschoenen, Scheenbeschermers, " +
                $"kruisbeschermers, Boksbandages en Mondstukken kopen.");
            Console.ResetColor();
            string nameProduct_1 = "Bokshandschoenen";
            string nameProduct_2 = "Scheenbeschermers";
            string nameProduct_3 = "Kruisbeschermers";
            float priceProduct_1 = 59.99f;
            float priceProduct_2 = 55.49f;
            float priceProduct_3 = 25.00f;
            int quantityOfProduct_1 = 20;
            int quantityOfProduct_2 = 20;
            int quantityOfProduct_3 = 15;
            string descriptionProduct_1 = "Rode kunstleren Bokshandschoenen";
            string descriptionProduct_2 = "Zwarte kunstleren Scheenbeschermers";
            string descriptionProduct_3 = "Witte synthetisch leer Kruisbeschermers";
            string production_info_1 = "De fabrikant voor  de bokshandschoenen is Everlast";
            string production_info_2 = "De fabrikant voor  de scheenbeschermers is Everlast";
            string production_info_3 = "De fabrikant voor  de kruisbeschermers is Venum";

            Console.Write($"Product 1: {nameProduct_1} Aantal:  {quantityOfProduct_1}" +
                $"\nProduct 2: {nameProduct_2} Aantal:  {quantityOfProduct_2}" +
                $"\nProduct 3: {nameProduct_3} Aantal:  {quantityOfProduct_3}");

            Console.Write("\n1. item x bekijken\n2. item x kopen");

            // 输入验证
            int firstUserOption;
            while (!int.TryParse(Console.ReadLine(), out firstUserOption) || firstUserOption < 1 || firstUserOption > 2)
            {
                Console.WriteLine("Ongeldige invoer! Kies aub 1 of 2.");
            }

            switch (firstUserOption)
            {
                case 1:
                    Console.WriteLine("Welke product wil je bekijken? ");
                    Console.WriteLine("kies uit 1, 2, 3 ");
                    int optionForProducts;
                    while (!int.TryParse(Console.ReadLine(), out optionForProducts) || optionForProducts < 1 || optionForProducts > 3)
                    {
                        Console.WriteLine("Ongeldige invoer! Kies aub 1, 2 of 3.");
                    }
                    Console.Clear();
                    switch (optionForProducts)
                    {
                        case 1:
                            Console.WriteLine($"Naam van het product: {nameProduct_1} " +
                                $"Prijs(in euros)= {priceProduct_1} " +
                                $"Aantal van het product: {quantityOfProduct_1}" +
                                $"\nBeschrijving van het product: {descriptionProduct_1}" +
                                $"\nExtra info over het product: {production_info_1}");
                            break;
                        case 2:
                            Console.WriteLine($"Naam van het product: {nameProduct_2} " +
                                $"Prijs(in euros)= {priceProduct_2} " +
                                $"Aantal van het product: {quantityOfProduct_2}" +
                                $"\nBeschrijving van het product: {descriptionProduct_2}" +
                                $"\nExtra info over het product: {production_info_2}");
                            break;
                        case 3:
                            Console.WriteLine($"Naam van het product: {nameProduct_3} " +
                                $"Prijs(in euros)= {priceProduct_3} " +
                                $"Aantal van het product: {quantityOfProduct_3}" +
                                $"\nBeschrijving van het product: {descriptionProduct_3}" +
                                $"\nExtra info over het product: {production_info_3}");
                            break;
                    }
                    break;
                case 2:
                    Console.WriteLine("Welk product wil je kopen? ");
                    int buyingOption;
                    while (!int.TryParse(Console.ReadLine(), out buyingOption) || buyingOption < 1 || buyingOption > 3)
                    {
                        Console.WriteLine("Ongeldige invoer! Kies aub 1, 2 of 3.");
                    }
                    Console.WriteLine("hoeveel wil je ervan kopen? ");
                    int optiequantityOfProduct;
                    while (!int.TryParse(Console.ReadLine(), out optiequantityOfProduct) || optiequantityOfProduct < 1)
                    {
                        Console.WriteLine("Ongeldige invoer! Voer een positief getal in.");
                    }

                    switch (buyingOption)
                    {
                        case 1:
                            if (optiequantityOfProduct > quantityOfProduct_1)
                            {
                                Console.ForegroundColor = ConsoleColor.Red;
                                Console.WriteLine($"Sorry, er zijn maar {quantityOfProduct_1} {nameProduct_1} in voorraad");
                                Console.ResetColor();
                            }
                            else
                            {
                                int newquantityOfProduct_1 = quantityOfProduct_1 - optiequantityOfProduct;
                                Console.ForegroundColor = ConsoleColor.Green;
                                string stockMsg = newquantityOfProduct_1 == 0 ? "Nu is de voorraad leeg." : $"Nieuwe voorraad: {newquantityOfProduct_1}";
                                Console.WriteLine($"Er zijn succesvol {optiequantityOfProduct} {nameProduct_1} gekocht! {stockMsg}");
                                Console.ResetColor();
                                float totalPrice = priceProduct_1 * optiequantityOfProduct;
                                float totalPriceBTW = totalPrice + totalPrice * 0.21f;
                                Console.WriteLine($"Totale prijs= {totalPrice} \nTotale prijs (incl. btw) = ");
                                Console.ForegroundColor = ConsoleColor.Red;
                                Console.Write($"{totalPriceBTW}");
                                Console.ResetColor();
                            }
                            break;
                        case 2:
                            if (optiequantityOfProduct > quantityOfProduct
相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 04:55:59