简体   繁体   English

如何减少此程序中使用的 C# 代码量?

[英]How can I reduce the amount of C# code used in this program?

So I'm trying to write a simple program for practicing purposes, I got this to work the way I want it but I still feel like I can reduce the amount of code used, any suggestions?所以我正在尝试编写一个用于练习目的的简单程序,我让它按照我想要的方式工作,但我仍然觉得我可以减少使用的代码量,有什么建议吗?

Thanks, in advance!提前致谢!

static void Main(string[] args)
        {
            string playerAnswer = Console.ReadLine();
            string potReward = "";
            string trueReward = calculateReward(playerAnswer, potReward);

            Console.WriteLine("Congratulations, you won {0}! ", trueReward);

        }

        private static string calculateReward(string answer, string reward)
        {
            if (answer == "1")
            {
                reward = "a cat";
            }
            else if (answer == "2")
            {
                reward = "power to rival Chuck Norris";
            }
            else if (answer == "3")
            {
                reward = "a burger";
            }
            else
            {
                reward = "nothing";
            }

            return reward;
        }

I get the feeling this is your graded homework, not just practice, in any case here is some help.我觉得这是你的分级作业,不仅仅是练习,无论如何这里有一些帮助。

There are plenty of excellent tutorials for free online, here is one example that assumes no previous coding or C# experience.网上有很多优秀的免费教程,这里是一个假设没有编码或 C# 经验的例子。

https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/intro-to-csharp/ https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/intro-to-csharp/

Some starting points might be to look at:一些起点可能是看:

switch statements切换语句

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/switch https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/switch


      int caseSwitch = 1;

      switch (caseSwitch)
      {
          case 1:
              Console.WriteLine("Case 1");
              break;
          case 2:
              Console.WriteLine("Case 2");
              break;
          default:
              Console.WriteLine("Default case");
              break;
      }

multiple variable assignment多变量赋值

https://www.dotnetperls.com/multiple-local-variable ( general C-style guides can often apply too, though stick to C# if unsure at all ) https://www.dotnetperls.com/multiple-local-variable一般的 C 风格指南通常也可以应用,但如果不确定就坚持使用 C#

string s = "dot", a = "net", m = "perls";

And are all your variables actually being used/useful?所有的变量都被实际使用/有用吗?

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/passing-value-type-parameters https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/passing-value-type-parameters

A value-type variable contains its data directly as opposed to a reference-type variable, which contains a reference to its data.值类型变量直接包含其数据,而不是引用类型变量,后者包含对其数据的引用。 Passing a value-type variable to a method by value means passing a copy of the variable to the method.将值类型变量按值传递给方法意味着将变量的副本传递给方法。 Any changes to the parameter that take place inside the method have no affect on the original data stored in the argument variable.在方法内部对参数进行的任何更改都不会影响存储在参数变量中的原始数据。 If you want the called method to change the value of the argument, you must pass it by reference, using the ref or out keyword.如果您希望被调用的方法更改参数的值,则必须使用 ref 或 out 关键字通过引用传递它。 You may also use the in keyword to pass a value parameter by reference to avoid the copy while guaranteeing that the value will not be changed.您还可以使用 in 关键字通过引用传递值参数以避免复制,同时保证值不会被更改。 For simplicity, the following examples use ref.为简单起见,以下示例使用 ref。


Also, if its a private function, see if actually needing to be separate, though likely it will be if part of larger solution/project.此外,如果它是一个私有函数,看看是否真的需要分开,尽管它可能是更大的解决方案/项目的一部分。

Ive deliberately not updated your sample code, very simple to do given examples and the more you type it out yourself the more you get used to the language.我故意不更新您的示例代码,给定示例非常简单,您自己输入的越多,您就越习惯该语言。

Hope that helps :)希望有帮助:)

You can also use the ternary operator for filter:您还可以使用三元运算符进行过滤:

    private static string calculateReward(string answer, string reward)
    {
        reward = answer == "1" ? "a cat" : answer == "2" ? "power to rival Chuck Norris" : answer == "3" ? "a burger" : "nothing";
        return reward;
    }

A switch-case statement is the best way to go on this one. switch-case 语句是进行此操作的最佳方式。

switch(answer) 
{
     case 1:
         reward = "A cat."; 
         break;
     case 2: 
         reward = "Power to rival Chuck Norris."; 
         break;
     case 3: 
         reward = "A burger"; 
         break;
     default: 
         reward = "Nothing"; 
         break;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM