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

C# 如何使用C将两个整数转换为一个整数?,c#,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");

如何使用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");                                                          .       
    }
` 不使用字符串。 谢谢

不能将int与int连接起来

您需要执行以下操作:Debug.log您还活着!+健康伤害+生命值

这样我们把两个整数组合成一个答案

这等同于写下以下内容:

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

如果您使用的是C6或7,那么字符串插值就是最好的选择


旧方法使用string.Format

Debug.Log (string.Format("You are alive! {0} Hitpoints",health-damageTaken));
新方法,在字符串前面使用“$”:

Debug.Log ($"You are alive! {health-damageTaken} Hitpoints");                                                                
编译时,它们是相同的

int health = 100;
int damageTaken = 50;
int remainingHealth = health - damageTaken;
bool alive = remainingHealth > 0;
if (alive) 
{
    Debug.Log ($"You are alive! {remainingHealth} Hitpoints");                                                        
}
对于C语言中的数学,您可以分别对加法、减法、除法和乘法执行:+、-、/、*

您还可以使用%来表示余数,例如3%2返回1


对于powers,使用Math.Pow,-例如,Math.Pow2,4给出了2^4 16的结果

你能更清楚地知道你在寻找什么吗?我已经回答了你下面的问题。作为一个新用户,我只想指出,对于像这样的简单问题,谷歌通常是一个很好的答案来源,甚至是复杂的问题。大多数时候,你会发现链接到现有的StackOverflow帖子,这些帖子回答了你的确切问题。
int health = 100;
int damageTaken = 50;
int remainingHealth = health - damageTaken;
bool alive = remainingHealth > 0;
if (alive) 
{
    Debug.Log ($"You are alive! {remainingHealth} Hitpoints");                                                        
}