- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一系列有点笨拙的 if 语句,按照习惯,我像这样嵌套:
// Cannot collide with right
if(direction.x < 0)
{
// Cannot collide with top
if(direction.y < 0)
{
// Check left and bottom
}
// Cannot collide with bottom
else if (direction.y > 0)
{
// Check left and top
}
// Cannot collide with top or bottom
else
{
// Check left only
}
}
// Cannot collide with left
else if (direction.x > 0)
{
// Cannot collide with top
if(direction.y < 0)
{
// Check right and bottom
}
// Cannot collide with bottom
else if (direction.y > 0)
{
// Check right and top
}
// Cannot collide with top or bottom
else
{
// Check right only
}
}
但是我发现它有点难以阅读,并且认为将其作为一组简单的 if 语句更容易理解,有点像 switch:
// Cannot collide with right or top
if(direction.x < 0 && direction.y < 0)
{
// Check left and bottom
}
// Cannot collide with right or bottom
else if(direction.x < 0 && direction.y > 0)
{
// Check left and top
}
// Cannot collide with right, top or bottom
else if(direction.x < 0)
{
// Check left only
}
// Cannot collide with left or top
else if (direction.x > 0 && direction.y < 0)
{
// Check right and bottom
}
// Cannot collide with left or bottom
else if (direction.x > 0 && direction.y > 0)
{
// Check right and top
}
// Cannot collide with left, top or bottom
else
{
// Check right only
}
这样做的明显缺点是我要多次重新检查某个条件。在这种情况下,它是如此之小,我无法想象它有什么区别,但我的问题是:
最佳答案
C# 中的 AND 语句会被编译器简化,因此如果 Direction.x <= 0,则 else if 语句中的第二个子句将不会被求值,因此不需要在“direction”的基础上嵌套> 0”。
关于java - 与平面 if 语句相比,嵌套 if 语句是否可以提高性能或具有其他优势?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27247530/
首先对WCF与WebService的定义进行概括,接着介绍了WCF的优势,最后就是对WCF与WebService两者的根本区别进行比较,具体内容如下 1、定义 1.WebService:严格来
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 5年前关闭。 Improve t
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 8 年前。 Improve
谁能告诉我将下一个脚本标记放在 元素之后和将它放在 元素之前有什么好处? (function (){ var script = document.createElement("script"); s
我最近遇到了一个 JavaScript 脚本,其中作者似乎试图避免在代码中使用字符串并将所有内容都分配给一个变量。 所以代替 document.addEventListener('click', (e
Scott Meyers 在“Effective Modern C++”中说表达式 Matrix sum = m1 + m2 + m3 + m4 (其中所有对象的类型均为 Matrix)“如果 ope
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
免责声明:我是网络开发的新手,所以请多多包涵... 堆栈:Angular 前端,后端的 Tornado(基于 python)网络服务器 我一直在使用 RxJs 和 WebSocket 成功地与后端通信
我一直在调查我的 Flutter 应用程序的 JSON 解析,并且有一个关于我无法解决的工厂构造函数的问题。我试图了解使用工厂构造函数与普通构造函数的优势。例如,我看到很多 JSON 解析示例,它们使
添加 SQL 后尝试打开 TadsQuery 时出现 5400 AE_INTERNAL_ERROR。当我将相同的 SQL 直接放在 TadsQuery 中时,没有错误。您的帮助文件指示我联系 Adva
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 8年前关闭。 Improve this
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 9 年前。 Improv
我想定义一个函数,当给定一个整数时,它将返回一个字符串。像这样: # Pseudocode function get_string(i) return my_string_array[i] end
我曾尝试在 Linux 和 Windows 上使用 DBD::Advantage,但没有成功。 Windows 版本附带了一个看似完整的安装程序,但它留下了 DBD-Advantage-8.10.ta
为什么说 NoSQL 在结构上比某些 SQL 数据库更快?假设我在某个 SQL 表的列上添加了一些索引。有人可以提供某种更快的查询吗? 我正在阅读有关 redis 的内容。 class User <
我的问题很简单:如果接口(interface)由单个类实现,那么使用接口(interface)有什么好处吗? 我一直认为只有当该接口(interface)有多个实现时,接口(interface)才是好
考虑这些(或多或少)等价的类型签名: f1 :: [a] -> Int -> a f2 :: Integral b => [a] -> b -> a f2 比 f1 更通用,这是一个很大的优势,但是
我们在 Scala 中部分应用了函数- def sum(a:Int,b:Int,c:Int) = a+b+c val partial1 = sum(1,_:Int,8) 我想知道使用部分应用函数的优点
你看下面的代码,我是如何使用 session 变量的;所以这三个问题是: 它们存放在哪里? (服务器或客户端) 它们对于每个网页访问者来说都是独一无二的吗? 我可以在完成工作后使用 ajax 或简单的
我知道这是一个常见问题,互联网上有关于此主题的资源,但我想从这个社区了解每个人在部署博客时对子域与子文件夹的想法(SEO 优势)。 谢谢。 最佳答案 我研究过一次,根据社区反馈以及权衡利弊,我会说去一
我是一名优秀的程序员,十分优秀!