C# 将负数结果返回为零。

huangapple go评论67阅读模式
英文:

C# return negative result into zero

问题

我希望如果总分为负数时,我的总结果返回0。

我尝试过以下的代码:

Double Apoints, Bpoints, Cpoints, Dpoints, Majortotalpoints, Totalmisses, Totalpoints.

Majortotalpoints = Apoints + Bpoints + Cpoints + Dpoints - Totalmisses;

Pointshandgun.text = Majortotalpoints.ToString();

Apoints = Convert.ToInt32(MajorApoints.text) * 5;

Bpoints = Convert.ToInt32(MajorBpoints.text) * 4;

Cpoints = Convert.ToInt32(MajorCpoints.text) * 4;

Dpoints = Convert.ToInt32(MajorDpoints.text) * 2;

Totalmisses = Convert.ToInt32(Totalmisshandgun.text) * 10;
英文:

I want my total result to return 0 if it's a negative total result.

I have tried the following

Double Apoints,Bpoints,Cpoints,Dpoints, Majortotalpoints,Totalmisses,Totalpoints.

Majortotalpoints=Apoints+Bpoints+Cpoints+Dpoints-Totalmisses;

Pointshandgun.text= Majortotalpoints.ToString();

Apoints =convert.int32(MajorApoints.text)\*5

Bpoints =convert.int32(MajorBpoints.text)\*4

Cpoints =convert.int32(MajorCpoints.text)\*4

Dpoints =convert.int32(MajorDpoints.text)\*2

Totalmisses =convert.int32(Totalmisshandgun.text)\*10;

答案1

得分: 1

return totalResult >= 0 ? totalResult : 0;

英文:

Assuming you put your total result into a variable called totalResult, you could check if that is greater than or equal to 0 (is positive) and if it is, return it, else return 0.

return totalResult >= 0 ? totalResult : 0;

答案2

得分: 0

return Math.Max(0, anyExpressionHere);

英文:

return Math.Max(0, anyExpressionHere);

huangapple
  • 本文由 发表于 2023年3月7日 04:06:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75655363.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定