-6

So i have a question. I'm trying do make a function witch returns a number, but the problem is that i can't convert int to string. My functions looks like this:

    static string EnemyDmg(EnemyDmg _dmg)
    {
        string result = "";
         int EnemyDmg
             = CharAttack - EnemyDefense;
        if (EnemyDmg < 1)
            EnemyDmg = 4;
        result = EnemyDmg;
        return result;
    }

but it should do this

     int EnemyDmg
          = CharAttack - EnemyDefense;
     if (EnemyDmg < 1)
     EnemyDmg = 4;
     Console.WriteLine(EnemyName + "  takes  " + EnemyDmg + "  Damage");

has anyone an idea?

PS: The 4 is just a random number.

3 Answers3

0

should be static int EnemyDmg(EnemyDmg _dmg). You should return an int, and convert to string outside the function iif you need that

Anyway, to convert a String s into an int i:

int i = Int32.Parse(s);

to convert an int i into a string s

string s = i.ToString();
string s = ""+i; // more "java-like"
Exceptyon
  • 1,584
  • 16
  • 23
  • `""+i` is more java like? Apart from the fact that it's irrelevant it is `Integer.toString(i)` http://stackoverflow.com/questions/4105331/how-to-convert-from-int-to-string – Tim Schmelter Apr 20 '16 at 15:21
0

This question is a bit ambiguous; I'm not sure why you've done it this way.

You can convert a C# integer to a string with the .ToString() method, like this:

int a = 12;
string aString = a.ToString();
Console.WriteLine(a);

https://dotnetfiddle.net/sMC3hU

Matthew Alltop
  • 501
  • 4
  • 20
0
    static string toStr(int intInput)
    {
        string str = intInput.ToString();
        return str;
    }

    }

This code will do it for you. There is no need to use if statement as there is no any specific requirement, it will make more complicated code.

or else

you can direct use ToString parameter if there is an user input just refer to the 3rd line.

Rutvik
  • 1