Home > Blockchain >  Does anyone can fix my learning C# code of randomNumn and writeOut?
Does anyone can fix my learning C# code of randomNumn and writeOut?

Time:02-11

I have a problem of my C# code. Here is an code that I tried:

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

namespace Console_test
{
    class Program
    {
        static void Main(string[] args)
        {
          Console.WriteLine(writeOut(5,4));
          Console.ReadLine();
        }

        static int randomNum()
        {
            return 4;
        }

        static int writeOut(int num1, int num2)
        {
            int total = num1   num2;
          Console.WriteLine(total);
        }
    }
}

I have a one error, can anyone fix my code? I'm not good with C#. Error says not all code paths returns a value.

CodePudding user response:

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

namespace Console_test
{
    class Program
    {
        static void Main(string[] args)
        {
          Console.WriteLine(writeOut(5,4));
          Console.ReadLine();
        }

        static int randomNum()
        {
            return 4;
        }

        static int writeOut(int num1, int num2)
        {
            return num1   num2;
          
        }
    }
}

CodePudding user response:

 static int writeOut(int num1, int num2)
        {
            int total = num1   num2;
          Console.WriteLine(total);
        }

This function is wrong. After the Static it says "int" this means your function must return an int. at the end of the function I think you want to add "Return total;" this will fix your error.

 static int writeOut(int num1, int num2)
        {
            int total = num1   num2;
          Console.WriteLine(total);
          return total;
        }
  • Related