【问题标题】:Trying to create a switch statement but doesnt work and there just appears a black window试图创建一个 switch 语句但不起作用,只出现一个黑色窗口
【发布时间】:2021-04-22 21:00:41
【问题描述】:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {



            string genre = "Horror";
            switch (genre)
            {
                case "Drama":
                    Console.WriteLine("Citizen Kane");
                    break;

                case "Comedy":
                    Console.WriteLine("Duck Soup");
                    break;

                case "Adventure":
                    Console.WriteLine("King Kong");
                    break;

                case "Horror":
                    Console.WriteLine("Psycho");
                    break;

                case "Science Fiction":
                    Console.WriteLine("2001: A Space Odyssey");
                    break;
                default:
                    Console.WriteLine("Movie not found");
                    break;

            }
        }
    }
}

它在 VS 上的 CodeCademy 上也不起作用(黑色窗口打开 1 秒然后关闭)有人知道如何解决这个问题吗?我试图将流派恐怖更改为 Console.ReadLine();但这也没有用

【问题讨论】:

  • 这能回答你的问题吗? How to keep console window open
  • 在 try.dot.net 上工作。只是您在退出查看输出之前没有等待吗?如果您从终端窗口运行它会怎样?
  • 确实,everything works fine (fiddle snippet),只需在末尾添加:Console.ReadKey(); 等待用户按键,否则控制台立即关闭。
  • 只需在 main 方法的末尾添加 console.Readline() 在它存在之前,你会看到控制台不会消失,直到你按下任何键

标签: c# switch-statement


【解决方案1】:

试试这个 - 在 Console.ReadLine() 中添加:

    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
    
    
    
                string genre = "Horror";
                switch (genre)
                {
                    case "Drama":
                        Console.WriteLine("Citizen Kane");
                        break;
    
                    case "Comedy":
                        Console.WriteLine("Duck Soup");
                        break;
    
                    case "Adventure":
                        Console.WriteLine("King Kong");
                        break;
    
                    case "Horror":
                        Console.WriteLine("Psycho");
                        break;
    
                    case "Science Fiction":
                        Console.WriteLine("2001: A Space Odyssey");
                        break;
                    default:
                        Console.WriteLine("Movie not found");
                        break;
    
                }
                Console.ReadLine();
            }
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-25
    • 2021-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-22
    相关资源
    最近更新 更多