英文:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论