- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我的应用程序的一部分有 6 个不同的输入,其中每个输入有 5 个不同的可能值。这可以计算出 15,625 个排列。我想相信我不必把每一个都编码出来,但我真的不确定。我已经开始编写它,就好像我必须编写出每一个代码一样,但这让我想到必须有更好的方法。所以我来这里是想看看是否有更有经验的编码人员知道更好的方法来做到这一点。
还有别的办法吗?除了必须编写出每个排列之外?
方程解释:有 6 个分支(输入)。每个输入取决于其剩余健康的百分比。这些相关性(以百分比表示)小于或等于 0、大于 0 且小于或等于 25、大于 25 且小于或等于 50、大于 50 且小于或等于 75,以及大于 75。每个排列通常会产生不同的输出。此输出显示在应用程序中供用户使用。
[编辑] 这不是伤害计算。这些是根据玩家受到的伤害程度对玩家统计数据施加的惩罚。预期结果要么不影响他们的统计数据,要么减少他们的统计数据。有时,不同变量减去的数字相同。如果您查看下面的代码,您会发现 head > 0.0 && head <= 0.25
的数字相同。和 chest > 0.0 && chest <= 0.25
,以及 chest > 0.25 && chest <= 0.50
和 leftarm > 0.25 && leftarm <= 0.50
和 rgitharm > 0.25 && rightarm <= 0.50
[编辑] 我会在这里回复评论:
@Frank N. Stein:糟糕。感谢指正:)
@aioobe:我编辑了 equation explanation
希望澄清预期的结果。
@ian:这些值无法计算,因为它们是百分比。我永远无法解决它们。
@Marco13:这似乎只是因为我还没有完成对每个排列的编码。转念一想,我觉得没必要装死。我可以将这些排列排除在计算之外,因为它是在不同的 Activity 中处理的。
@aioobe:这是否比@Ian 和@Durandal 建议的枚举结果更好。
@Danil Gaponov:这比@Durandal 在他/她的回答中建议的更好还是更差?
@Durandal:我已经更新了 equation explanation
希望更清楚地了解我对这些结果中的每一个的期望。
感谢您的耐心等待:)
我能做类似这样的事情吗:
// HEALTH
// N > 0.75 == Full
// N > 0.50 && <= 0.75 == HighHealth
// N > 0.25 && <= 0.50 == MedHealth
// N > 0.00 && <= 0.25 == LowHealth
// N <= 0.00 == Dead
IE 然后我可以写 h == Full
或 h == Health.Full
.这会让我的生活更轻松,也会让代码更容易阅读。虽然,我仍然需要计算出每一个排列,但它会更易于管理。
我的另一个想法是将这些计算放在不同的类中并在 Activity 中运行。像这样:
// SETTING DATA
// WEAPON 1
// MELEE
weapon1attack.setText(PenaltiesMeleeHelper);
weapon1accuracy.setText(PenaltiesMeleeHelper);
// RANGED
weapon1thrownattack.setText(PenaltiesThrownHelper);
weapon1thrownaccuracy.setText(PenaltiesThrownHelper);
// WEAPON 2
// MELEE
weapon2attack.setText(PenaltiesMeleeHelper);
weapon2accuracy.setText(PenaltiesMeleeHelper);
// RANGED
weapon2thrownattack.setText(PenaltiesThrownHelper);
weapon2thrownaccuracy.setText(PenaltiesThrownHelper);
这些有可能吗?
[编辑] 数学算法有用吗?假设我有 Checker,它会在每次 Activity 开始时检查处罚。如果它发现一个/多个罚球,那么它会为我将它们全部加在一起并从适当的统计数据中减去它们。
例如:
// There are 3 main outcomes that are suffered for being damaged too much
// The reduction of parry (PRY), dodge (DOD), and damage & accuracy (AM)
// The deductions range from -2 to -5.
// I could declare Enums for each type
PRY2 = (- 2)
DOD2 = (- 2)
AM2 = (- 2)
PRY2 would be put into the equation if la > 0.50 && la <= 0.75 || ra > 0.50 && ra <= 0.75
DOD2 would be put into the equation if la > 0.50 && la <= 0.75 || ra > 0.50 && ra <= 0.75 || ll > 0.50 && ll <= 0.75 || rl > 0.50 && rl <= 0.75
AM2 would be put into the equation if la > 0.50 && la <= 0.75 || ra > 0.50 && ra <= 0.75 || c > 0.50 && c <= 0.75
PRY3 = (- 3)
DOD3 = (- 3)
AM3 = (- 3)
PRY3 would be put into the equation if c > 0.25 && c <= 0.50 || la > 0.25 && la <= 0.50 || ra > 0.25 && ra <= 0.50 ||
DOD3 would be put into the equation if c > 0.25 && c <= 0.50 || la > 0.25 && la <= 0.50 || ra > 0.25 && ra <= 0.50 || ll > 0.25 && ll <= 0.50 || rl > 0.25 && rl <= 0.50
AM3 would be put into the equation if c > 0.25 && c <= 0.50 || la > 0.25 && la <= 0.50 || ra > 0.25 && ra <= 0.50 ||
PRY4 = (- 4)
DOD4 = (- 4)
AM4 = (- 4)
PRY4 would be put into the equation if la > 0.0 && la <= 0.25 || ra > 0.0 && ra <= 0.25 || ll > 0.0 && ll <= 0.25 || rl > 0.0 && rl <= 0.25
DOD4 would be put into the equation if la > 0.0 && la <= 0.25 || ra > 0.0 && ra <= 0.25 || ll > 0.0 && ll <= 0.25 || rl > 0.0 && rl <= 0.25
AM4 would be put into the equation if la > 0.0 && la <= 0.25 || ra > 0.0 && ra <= 0.25 || ll > 0.0 && ll <= 0.25 || rl > 0.0 && rl <= 0.25
PRY5 = (- 5)
DOD5 = (- 5)
AM5 = (- 5)
PRY5 would be put into the equation if h > 0.0 && h <= 0.25 || c > 0.0 && c <= 0.25 || la <= 0.0 || ra <= 0.0 || ll <= 0.0 || rl <= 0.0
DOD5 would be put into the equation if h > 0.0 && h <= 0.25 || c > 0.0 && c <= 0.25 || la <= 0.0 || ra <= 0.0 || ll <= 0.0 || rl <= 0.0
AM5 would be put into the equation if h > 0.0 && h <= 0.25 || c > 0.0 && c <= 0.25 || la <= 0.0 || ra <= 0.0 || ll <= 0.0 || rl <= 0.0
// I'm not sure how, but somehow it would know, since it's PRY, then it knows to take the Enum and deduct it from the PRY slot and not the DOD or AM slots.
// Is this a step in the right direction?
这是我目前的代码(编码出每个排列):
// SETTING DATA
// WEAPON 1
// MELEE
try {
float STRF = NumberUtils.toFloat(pref.getString("strength", ""), 0.0f);
float MCF = NumberUtils.toFloat(pref.getString("melee", ""), 0.0f);
float W1AttF = NumberUtils.toFloat(pref.getString("w1attack", ""), 0.0f);
// ALL LIMBS > 0.00 && h <= 0.25
if (MCF != 0.0 && W1AttF != 0.0 && h > 0.0 && h <= 0.25 && c > 0.0 && c <= 0.25
&& la > 0.0 && la <= 0.25 && ra > 0.0 && ra <= 0.25 && ll > 0.0 && ll <= 0.25
&& rl > 0.0 && rl <= 0.25) {
weapon1attack.setText(Float.toString((STRF + MCF + W1AttF) - 26));
}
// HEAD h > 0.25 && h <= 0.50
else if (MCF != 0.0 && W1AttF != 0.0 && h > 0.25 && h <= 0.50 && c > 0.0 && c <= 0.25
&& la > 0.0 && la <= 0.25 && ra > 0.0 && ra <= 0.25 && ll > 0.0 && ll <= 0.25
&& rl > 0.0 && rl <= 0.25) {
weapon1attack.setText(Float.toString((STRF + MCF + W1AttF) - 21));
}
// HEAD h > 0.50 && h <= 0.75
else if (MCF != 0.0 && W1AttF != 0.0 && h > 0.50 && h <= 0.75 && c > 0.0 && c <= 0.25
&& la > 0.0 && la <= 0.25 && ra > 0.0 && ra <= 0.25 && ll > 0.0 && ll <= 0.25
&& rl > 0.0 && rl <= 0.25) {
weapon1attack.setText(Float.toString((STRF + MCF + W1AttF) - 21));
}
// HEAD h > 0.75
else if (MCF != 0.0 && W1AttF != 0.0 && h > 0.75 && c > 0.0 && c <= 0.25
&& la > 0.0 && la <= 0.25 && ra > 0.0 && ra <= 0.25 && ll > 0.0 && ll <= 0.25
&& rl > 0.0 && rl <= 0.25) {
weapon1attack.setText(Float.toString((STRF + MCF + W1AttF) - 21));
}
// CHEST c > 0.25 && c <= 0.50
else if (MCF != 0.0 && W1AttF != 0.0 && h > 0.0 && h <= 0.25 && c > 0.25 && c <= 0.50
&& la > 0.0 && la <= 0.25 && ra > 0.0 && ra <= 0.25 && ll > 0.0 && ll <= 0.25
&& rl > 0.0 && rl <= 0.25) {
weapon1attack.setText(Float.toString((STRF + MCF + W1AttF) - 26));
}
// CHEST c > 0.50 && c <= 0.75
else if (MCF != 0.0 && W1AttF != 0.0 && h > 0.0 && h <= 0.25 && c > 0.50 && c <= 0.75
&& la > 0.0 && la <= 0.25 && ra > 0.0 && ra <= 0.25 && ll > 0.0 && ll <= 0.25
&& rl > 0.0 && rl <= 0.25) {
weapon1attack.setText(Float.toString((STRF + MCF + W1AttF) - 26));
}
// CHEST c > 0.75
else if (MCF != 0.0 && W1AttF != 0.0 && h > 0.0 && h <= 0.25 && c > 0.75
&& la > 0.0 && la <= 0.25 && ra > 0.0 && ra <= 0.25 && ll > 0.0 && ll <= 0.25
&& rl > 0.0 && rl <= 0.25) {
weapon1attack.setText(Float.toString((STRF + MCF + W1AttF) - 26));
}
// HEAD ONLY
else if (MCF != 0.0 && W1AttF != 0.0 && h > 0.0 && h <= 0.25) {
weapon1attack.setText(Float.toString((STRF + MCF + W1AttF) - 5));
} else if (MCF != 0.0 && W1AttF != 0.0 && h <= 0.0) {
weapon1attack.setText("Dead");
}
// CHEST ONLY
else if (MCF != 0.0 && W1AttF != 0.0 && c > 0.0 && c <= 0.25) {
weapon1attack.setText(Float.toString((STRF + MCF + W1AttF) - 5));
} else if (MCF != 0.0 && W1AttF != 0.0 && c > 0.25 && c <= 0.50) {
weapon1attack.setText(Float.toString((STRF + MCF + W1AttF) - 3));
} else if (MCF != 0.0 && W1AttF != 0.0 && c > 0.50 && c <= 0.75) {
weapon1attack.setText(Float.toString((STRF + MCF + W1AttF) - 2));
} else if (MCF != 0.0 && W1AttF != 0.0 && c <= 0.0) {
weapon1attack.setText("Dead");
}
// LEFT ARM ONLY
else if (MCF != 0.0 && W1AttF != 0.0 && la > 0.0 && la <= 0.25) {
weapon1attack.setText(Float.toString((STRF + MCF + W1AttF) - 4));
} else if (MCF != 0.0 && W1AttF != 0.0 && la > 0.25 && la <= 0.50) {
weapon1attack.setText(Float.toString((STRF + MCF + W1AttF) - 3));
} else if (MCF != 0.0 && W1AttF != 0.0 && la > 0.50 && la <= 0.75) {
weapon1attack.setText(Float.toString((STRF + MCF + W1AttF) - 2));
} else if (MCF != 0.0 && W1AttF != 0.0 && la <= 0.0) {
weapon1attack.setText("Dead");
}
// RIGHT ARM ONLY
else if (MCF != 0.0 && W1AttF != 0.0 && ra > 0.0 && ra <= 0.25) {
weapon1attack.setText(Float.toString((STRF + MCF + W1AttF) - 4));
} else if (MCF != 0.0 && W1AttF != 0.0 && ra > 0.25 && ra <= 0.50) {
weapon1attack.setText(Float.toString((STRF + MCF + W1AttF) - 3));
} else if (MCF != 0.0 && W1AttF != 0.0 && ra > 0.50 && ra <= 0.75) {
weapon1attack.setText(Float.toString((STRF + MCF + W1AttF) - 2));
} else if (MCF != 0.0 && W1AttF != 0.0 && ra <= 0.0) {
weapon1attack.setText("Dead");
}
// LEFT LEG ONLY
else if (MCF != 0.0 && W1AttF != 0.0 && ll > 0.0 && ll <= 0.25) {
weapon1attack.setText(Float.toString((STRF + MCF + W1AttF) - 4));
} else if (MCF != 0.0 && W1AttF != 0.0 && ll <= 0.0) {
weapon1attack.setText("Dead");
}
// RIGHT LEG ONLY
else if (MCF != 0.0 && W1AttF != 0.0 && rl > 0.0 && rl <= 0.25) {
weapon1attack.setText(Float.toString((STRF + MCF + W1AttF) - 4));
} else if (MCF != 0.0 && W1AttF != 0.0 && rl <= 0.0) {
weapon1attack.setText("Dead");
}
else if (W1AttF == 0.0 || MCF == 0.0) {
weapon1attack.setText("");
} else if (MCF != 0.0 && W1AttF != 0.0) {
weapon1attack.setText(Float.toString(STRF + MCF + W1AttF));
}
}
catch (NumberFormatException ignore) {}
最佳答案
每个案例的期望结果看起来都太少了。从代码中的评论中,我收集了它的某种 RPG 伤害计算。
最好先用自然语言描述问题,或者更确切地说不是问题,而是控制每个参数组合结果的规则。查看从规则中出现的模式,通常可以定义一个相当小的案例集,这些案例可以用数学公式表示。
如果你真的想完全自由地控制每个案例的结果,你需要枚举案例,并构建一个包含每个可能案例结果的数据结构(例如,数据文件 列出以某种形式读入内存的每个组合的结果。
根据我在您的代码中看到的内容,我假设您正在尝试根据位置、武器、防御等计算伤害。
这很适合使用简单的数学公式,例如首先确定位置,并根据该位置分配一个 dmanage 修饰符(伪代码):
modifier = 1.0; // defaults to "normal" damager
switch (location) {
case HEAD:
modfier *= 2.00; // double damage
case CHEST:
modifier *= 0.75; // 75% damage
default:
// elsewhere: no change
break;
}
// ... further modify based on other parameters like attack
// type, defense type whatever you have
// now figure out a base damage, purely fictional
baseDmg = attack - defense;
// we don't want negative damage in case def > att,
// so if negative set to 1.0
baseDmg = Math.max(baseDmg, 1.0);
// caluclate final dmg using modifier
realDmg = baseDmg * modifier;
if (realDmg > life)
// dead!
else
// ouch!
试着分解你的规则,这样你就可以像上面的例子那样写了。
关于java - 数学排列码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26256374/
我正在编写一个具有以下签名的 Java 方法。 void Logger(Method method, Object[] args); 如果一个方法(例如 ABC() )调用此方法 Logger,它应该
我是 Java 新手。 我的问题是我的 Java 程序找不到我试图用作的图像文件一个 JButton。 (目前这段代码什么也没做,因为我只是得到了想要的外观第一的)。这是我的主课 代码: packag
好的,今天我在接受采访,我已经编写 Java 代码多年了。采访中说“Java 垃圾收集是一个棘手的问题,我有几个 friend 一直在努力弄清楚。你在这方面做得怎么样?”。她是想骗我吗?还是我的一生都
我的 friend 给了我一个谜语让我解开。它是这样的: There are 100 people. Each one of them, in his turn, does the following
如果我将使用 Java 5 代码的应用程序编译成字节码,生成的 .class 文件是否能够在 Java 1.4 下运行? 如果后者可以工作并且我正在尝试在我的 Java 1.4 应用程序中使用 Jav
有关于why Java doesn't support unsigned types的问题以及一些关于处理无符号类型的问题。我做了一些搜索,似乎 Scala 也不支持无符号数据类型。限制是Java和S
我只是想知道在一个 java 版本中生成的字节码是否可以在其他 java 版本上运行 最佳答案 通常,字节码无需修改即可在 较新 版本的 Java 上运行。它不会在旧版本上运行,除非您使用特殊参数 (
我有一个关于在命令提示符下执行 java 程序的基本问题。 在某些机器上我们需要指定 -cp 。 (类路径)同时执行java程序 (test为java文件名与.class文件存在于同一目录下) jav
我已经阅读 StackOverflow 有一段时间了,现在我才鼓起勇气提出问题。我今年 20 岁,目前在我的家乡(罗马尼亚克卢日-纳波卡)就读 IT 大学。足以介绍:D。 基本上,我有一家提供簿记应用
我有 public JSONObject parseXML(String xml) { JSONObject jsonObject = XML.toJSONObject(xml); r
我已经在 Java 中实现了带有动态类型的简单解释语言。不幸的是我遇到了以下问题。测试时如下代码: def main() { def ks = Map[[1, 2]].keySet()
一直提示输入 1 到 10 的数字 - 结果应将 st、rd、th 和 nd 添加到数字中。编写一个程序,提示用户输入 1 到 10 之间的任意整数,然后以序数形式显示该整数并附加后缀。 public
我有这个 DownloadFile.java 并按预期下载该文件: import java.io.*; import java.net.URL; public class DownloadFile {
我想在 GUI 上添加延迟。我放置了 2 个 for 循环,然后重新绘制了一个标签,但这 2 个 for 循环一个接一个地执行,并且标签被重新绘制到最后一个。 我能做什么? for(int i=0;
我正在对对象 Student 的列表项进行一些测试,但是我更喜欢在 java 类对象中创建硬编码列表,然后从那里提取数据,而不是连接到数据库并在结果集中选择记录。然而,自从我这样做以来已经很长时间了,
我知道对象创建分为三个部分: 声明 实例化 初始化 classA{} classB extends classA{} classA obj = new classB(1,1); 实例化 它必须使用
我有兴趣使用 GPRS 构建车辆跟踪系统。但是,我有一些问题要问以前做过此操作的人: GPRS 是最好的技术吗?人们意识到任何问题吗? 我计划使用 Java/Java EE - 有更好的技术吗? 如果
我可以通过递归方法反转数组,例如:数组={1,2,3,4,5} 数组结果={5,4,3,2,1}但我的结果是相同的数组,我不知道为什么,请帮助我。 public class Recursion { p
有这样的标准方式吗? 包括 Java源代码-测试代码- Ant 或 Maven联合单元持续集成(可能是巡航控制)ClearCase 版本控制工具部署到应用服务器 最后我希望有一个自动构建和集成环境。
我什至不知道这是否可能,我非常怀疑它是否可能,但如果可以,您能告诉我怎么做吗?我只是想知道如何从打印机打印一些文本。 有什么想法吗? 最佳答案 这里有更简单的事情。 import javax.swin
我是一名优秀的程序员,十分优秀!