本文介绍了有没有办法缩短它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好奇,如果这可能是1行或更短。基本上,我正在制作一个基于文本的游戏,您可以将现金或硬币存入银行,我的代码是:



Just curious if this could be like into 1 line or shorter. Basically, I'm making a text-based game and you can deposit 'cash' or 'coins' into the 'bank' and the code i have is:

 int deposited = int.Parse(msg[1]);
stats[user].Cash = stats[user].Cash - deposited;
stats[user].Bank = stats[user].Bank + deposited;





只是想知道它是否可以缩短



我的尝试:





Just wondering if it could be shortened

What I have tried:

 int deposited = int.Parse(msg[1]);
stats[user].Cash = stats[user].Cash - deposited;
stats[user].Bank = stats[user].Bank + deposited;

推荐答案

int deposited = 0;
if (int.TryParse(msg[1], out deposited))
{
    stats[user].Cash -= deposited;
    stats[user].Bank += deposited;
}
else
{
    throw some error to indicate the input was not valid
}


这篇关于有没有办法缩短它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 05:46