如何使用C#将两个整数转换为一个?

时间:2017-03-12 04:37:13

标签: c#

如何使用C#将两个整数转换为一个?不使用字符串。像

int health = 100;
    int damageTaken = 50;
    bool alive= true;
    if (alive == true){
    Debug.Log ("You are alive!" + ????(what goes here? I cant put health -
    damageTaken + "Hitpoints");                                                          .       
    }

` 不使用字符串。 谢谢!

4 个答案:

答案 0 :(得分:0)

您无法将int与int连接。

您需要执行:Debug.Log("You are alive! " + (health-damageTaken) + " Hitpoints.");

通过这种方式,我们可以结合使用"这两个整数成了一个答案。

这相当于写下这个:

int health = 100;
int damageTaken = 50;
int hitpoints = health - damageTaken;
Debug.Log("You are alive! " + hitpoints + " Hitpoints.")

答案 1 :(得分:0)

如果您使用的是C#6或7,则可以使用字符串插值。 https://msdn.microsoft.com/en-us/library/dn961160.aspx

 int health = 100;
 int damageTaken = 50;
 bool alive = true;
 if (alive == true)
 {
     Debug.Log($"You are alive! {health-damageTaken} HitPoints");
 }

答案 2 :(得分:0)

旧方式使用string.Format

Debug.Log (string.Format("You are alive! {0} Hitpoints",health-damageTaken));

新方法,使用字符串前面的'$':

Debug.Log ($"You are alive! {health-damageTaken} Hitpoints");                                                                

编译时它们是相同的

答案 3 :(得分:0)

constructor(private environmentService: EnvironmentService,private _ngzone:NgZone) {
    this.environmentService.getVersionInfo();

    ipcRenderer.on('environment-reply', (event, arg) => {
      console.log(arg);
      this._ngzone.run(()=>{
         this.version = arg;
      });
    });
  }

对于C#中的数学,你可以这样做:+, - ,/,*分别用于加,减,除和乘。

你还有%,它给你余数(例如3%2返回1)。

对于权力,使用Math.Pow(,) - 例如,Math.Pow(2,4)给出结果2 ^ 4(16)