简体   繁体   中英

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.

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


      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 )

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

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. 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. For simplicity, the following examples use 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(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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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