C#中使用语句和循环来控制程序流总程序....

简介: 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chinahuyong/article/details/2896261 using System;using System.
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chinahuyong/article/details/2896261

using System;
using System.Collections.Generic;
using System.Text;

namespace WebSites1
{
    class Program
    {  

        //Program entry
        static int Main(string[] args)
        {
            string[] siteName = new string[6];
            string phrase = "What is your pleasure";
            string choice;
            int count = 0;

            //If there was a cmd line arg,use it;
            if (args.Length == 0)
            {
                Console.WriteLine("{0},Master?",phrase);
            }
            else
            {
                Console.WriteLine("{0},{1}?",phrase,args[0]);
            }

            do
            {
                //Print menu.
                Console.WriteLine("");
                Console.WriteLine("A - Add Site");
                Console.WriteLine("S - Sort List");
                Console.WriteLine("R - Show Report /n");
                Console.WriteLine("Q - Quit/n");
                Console.WriteLine("Please Choose(A/S/R/Q): ");

                choice = Console.ReadLine();

                //Figure out what user wanted.
                switch(choice)
                {
                    //Add a site
                    case "a":
                    case "A":
                        Console.WriteLine("/nAdding Site/n");
                        string doAgain = "Y";

                        //Keep it up as long as user wants
                        while(doAgain.ToUpper() == "Y")
                        {
                            Console.Write(
                                "Please Enter Site Name: ");
                              siteName[count++]=Console.ReadLine();

                            Console.Write("Add Another?:");
                            doAgain=Console.ReadLine();

                            //There can only by 5 items
                            if(count>=5)
                            {
                               
                                break;

                            }
            
                        }
                        break;
                        //Sort the site list
                    case "s":
                    case "S":
                        Console.WriteLine("Sorting List....");

                        int n = siteName.Length - 2;
                        int j,k;
                        string save;

                        //Insertion sort,start at end & move up
                        for (k = n - 1; k >= 0; k--)
                        {
                            j = k + 1;
                            save = siteName[k];
                            //Sentinel makes inner
                            //loop more efficient
                            siteName[n + 1] = save;

                            //Insert siteName[k] into its sorted position
                            while (string.Compare(save, siteName[j]) > 0)
                            {
                                siteName[j - 1] = siteName[j];
                                j++;
                            }
                            siteName[j - 1] = save;

                        }
                        //clean out sentinel so it's not printed
                        siteName[siteName.Length-1] = null;

                        Console.WriteLine("Done sorting");
                        break;
                        //Print a report
                    case "r":
                    case "R":
                        string filter = "";
                        string response = "";

                        //If user wants to filter,get filter string
                        Console.Write("Would you like a Filter?");
                        response=Console.ReadLine();

                        if(response.ToUpper() =="Y")
                        {
                            Console.Write("/nPlease enter a filter:");
                            filter = Console.ReadLine();
                        }

                        Console.WriteLine("");
                        Console.WriteLine("Site Report");
                        Console.WriteLine("");

                        //Process every entry in siteName
                        foreach(string site in siteName)
                        {
                            //Execute filter
                            if(response.ToUpper() == "Y" && site != null & site.IndexOf(filter) == -1)
                            {
                                continue;
                            }

                            //Print non-filtered items
                            Console.WriteLine("/t {0}",site);
                      }
                        break;

                        //Exit Program

                    case "q":
                    case "Q":
                        Console.WriteLine("GoodBye");
                        break;

                        //User entered bad data
                    default:
                        Console.WriteLine("Huh??");
                        break;
              }//end swith

             //keep going until user wants to quit
         }while((choice = choice.ToUpper()) != "Q");

            return 0;
        }
    }
}

相关文章
|
29天前
|
安全 编译器 程序员
C# 中 foreach 循环和 for 循环深度比较
为什么建议你多数情况下使用 foreach 进行遍历循环?看完你就明白了
|
2月前
|
缓存 C# Windows
C#程序如何编译成Native代码
【10月更文挑战第15天】在C#中,可以通过.NET Native和第三方工具(如Ngen.exe)将程序编译成Native代码,以提升性能和启动速度。.NET Native适用于UWP应用,而Ngen.exe则通过预编译托管程序集为本地机器代码来加速启动。不过,这些方法也可能增加编译时间和部署复杂度。
105 2
|
2月前
|
Java C#
如何避免在C#循环中使用await
如何避免在C#循环中使用await
110 9
|
6月前
|
存储 安全 Java
程序与技术分享:C#值类型和引用类型的区别
程序与技术分享:C#值类型和引用类型的区别
45 0
|
2月前
|
设计模式 程序员 C#
C# 使用 WinForm MDI 模式管理多个子窗体程序的详细步骤
WinForm MDI 模式就像是有超能力一般,让多个子窗体井然有序地排列在一个主窗体之下,既美观又实用。不过,也要小心管理好子窗体们的生命周期哦,否则一不小心就会出现一些意想不到的小bug
122 0
|
2月前
|
XML 存储 安全
C#开发的程序如何良好的防止反编译被破解?ConfuserEx .NET混淆工具使用介绍
C#开发的程序如何良好的防止反编译被破解?ConfuserEx .NET混淆工具使用介绍
66 0
|
2月前
|
安全 API C#
C# 如何让程序后台进程不被Windows任务管理器强制结束
C# 如何让程序后台进程不被Windows任务管理器强制结束
65 0
|
3月前
|
C# 容器
C#中的命名空间与程序集管理
在C#编程中,`命名空间`和`程序集`是组织代码的关键概念,有助于提高代码的可维护性和复用性。本文从基础入手,详细解释了命名空间的逻辑组织方式及其基本语法,展示了如何使用`using`指令访问其他命名空间中的类型,并提供了常见问题的解决方案。接着介绍了程序集这一.NET框架的基本单位,包括其创建、引用及高级特性如强名称和延迟加载等。通过具体示例,展示了如何创建和使用自定义程序集,并提出了针对版本不匹配和性能问题的有效策略。理解并善用这些概念,能显著提升开发效率和代码质量。
106 4
|
2月前
|
API C#
C#实现Winform程序右下角弹窗消息提示
C#实现Winform程序右下角弹窗消息提示
90 0
|
3月前
|
Linux C# 开发者
Uno Platform 驱动的跨平台应用开发:从零开始的全方位资源指南与定制化学习路径规划,助您轻松上手并精通 C# 与 XAML 编程技巧,打造高效多端一致用户体验的移动与桌面应用程序
【9月更文挑战第8天】Uno Platform 的社区资源与学习路径推荐旨在为初学者和开发者提供全面指南,涵盖官方文档、GitHub 仓库及社区支持,助您掌握使用 C# 和 XAML 创建跨平台原生 UI 的技能。从官网入门教程到进阶技巧,再到活跃社区如 Discord,本指南带领您逐步深入了解 Uno Platform,并提供实用示例代码,帮助您在 Windows、iOS、Android、macOS、Linux 和 WebAssembly 等平台上高效开发。建议先熟悉 C# 和 XAML 基础,然后实践官方教程,研究 GitHub 示例项目,并积极参与社区讨论,不断提升技能。
98 2