我有一些不同的单选按钮,可以返回种族和性别.该脚本在内部应用程序内部运行,因此不是返回"boy","girl"或"both",而是返回7707330,7707333和7707336.类似于种族单选按钮.
然后,我需要根据种族和性别的组合验证数据.这是一个非常简单的任务,但我最终得到了15个if语句!一切都按预期工作,但必须有一个更清洁的解决方案?
function test(radioResults) {
var1 = radioResults[0].toString();
var2 = radioResults[1].toString();
var roll = parseFloat(parent.roll);
if (var2 == '7707330') {
gender = 'boy';
}
if (var2 == '7707333') {
gender = 'girl';
}
if (var2 == '7707336') {
gender = 'both';
}
if (var1 == '7707341') {
maori(gender);
}
if (var1 == '7707344') {
pasifika(gender);
}
if (var1 == '7707347') {
all(gender);
}
}
function maori(gender) {
//Maori
if (gender == 'boy') {
ethnicity = parseFloat(parent.getMBoys);
validation(ethnicity);
}
if (gender …Run Code Online (Sandbox Code Playgroud) 我想在我的程序中制作更清晰的代码.所以我试图压缩我的代码来创建按钮:
以前,我需要每次复制一次:
Dimension JButton_Cryption_Size = JButton_Cryption.getPreferredSize();
JButton_Cryption.setBounds(5, 5, JButton_Cryption_Size.width + 50, JButton_Cryption_Size.height);
JButton_Cryption.setFocusPainted(false);
JButton_Cryption.addActionListener(this);
add(JButton_Cryption);
Run Code Online (Sandbox Code Playgroud)
但现在我做了这个方法:(不要注意按钮名称,它们是用于测试)
public JButton JButton_Testing1,
JButton_Testing2,
JButton_3;
private void addJButton(JButton ButtonName, String Name, int x, int y, int width, int height, String ToolTip, boolean FocusedPainted, boolean Opaque, boolean ContentAreaFilled, boolean BorderPainted){
ButtonName = new JButton(Name);
Dimension Button_Size = ButtonName.getPreferredSize();
if(width == 0){
ButtonName.setBounds(x, y, Button_Size.width, height);
}if(height == 0){
ButtonName.setBounds(x, y, width, Button_Size.height);
}if(width == 0 && height == 0){
ButtonName.setBounds(x, y, Button_Size.width, Button_Size.height);
}if(width != …Run Code Online (Sandbox Code Playgroud) 我应该清理一个java代码,摆脱了很多东西,但应该还有什么要清理,也许以某种方式摆脱多个if语句,而不完全重写这段代码?似乎无法弄清楚它们是如此不同,以便将它们堆叠在一个'if'中.有任何想法吗?
public class Calc {
// employee types
public static final int SELLER;
public static final int COOK;
public static final int CHIEF;
public static void main(final String[] args) {
Calc c = new Calc();
System.err.println(c.pay(CHIEF) + " should be 66");
}
private int pay(final int type, final int h) {
int Sum = 0;
if (type == SELLER) {
if (h > 8) {
Sum = 20 * (h - 8);
Sum += 80;
} else {
Sum += …Run Code Online (Sandbox Code Playgroud) 所以我完全使用 isPresent 而不是使用 == null 来检查对象是否成功返回,但我觉得我陷入了用 isPresent 乱丢代码的坑。
所以假设我有一堆不同的端点来检索或更新模型。我希望在他们每个人的开头都没有 isPresent 检查这个对象是否存在!
例子:
Optional<Object> myObject = objectRegistry.get(name);
if (myObject.isPresent()) {
doSomething();
} else {
throw new ObjectNotFoundException(stampName);
}
Run Code Online (Sandbox Code Playgroud)
我正在寻找解决这种乱扔垃圾的最佳实践,我可以想象其中一种解决方案是使用一种方法来执行此检查,并且我可以随时调用它,而调用它的其他方法将不得不抛出 ObjectNotFoundException
我尝试提交我的代码,但 IntelliJ 中的代码分析显示:'if' 语句可以在这一行中简化:
if (legendTitle != null && legendTitle.equals("My house"))
Run Code Online (Sandbox Code Playgroud)
LegendTitle 是一个字符串,我这样写legendTitle != null是因为.equals在一个空字符串中是一个 NullPointerException
我正在创建 RTS 游戏,其中一项功能是构建不同类型的建筑物。我发现了很多重复,我想在辅助方法中提取它,但问题是每个建筑物都是不同的对象,它们从主建筑物类中吸收了一些属性。
构建方法如下所示:
public static void buildDockyard(Base base) {
if (Validator.checkForBuilding(base, "Dockyard")) {
throw new IllegalStateException("Dockyard is already build");
}
Dockyard dockyard = new Dockyard("Dockyard");
int requiredPower = dockyard.requiredResource("power");
int requiredStardust = dockyard.requiredResource("stardust");
int requiredPopulation = dockyard.requiredResource("population");
Validator.checkResource(base, requiredPower, requiredStardust, requiredPopulation);
updateResourceAfterBuild(base, requiredPower, requiredStardust, requiredPopulation);
dockyard.setCompleteTime(dockyard.requiredResource("time"));
base.getBuildings().add(dockyard);
}
public static void buildHotel(Base base) {
if (Validator.checkForBuilding(base, "Space Hotel")) {
throw new IllegalStateException("Space Hotel is already build");
}
SpaceHotel spaceHotel = new SpaceHotel("Space Hotel");
int requiredPower = spaceHotel.requiredResource("power");
int requiredStardust …Run Code Online (Sandbox Code Playgroud) 有人会帮我清理这个as3代码,我已经连续几个小时都在研究这个代码了.它应该是一个火焰喷射器.如果你不能这样做或者不想这样做,那么有人可以给我一个链接到一个网站的链接几乎就像鼠标移动的东西和东西的意思.
code-cleanup ×7
java ×5
coding-style ×1
dry ×1
if-statement ×1
intellij-14 ×1
javascript ×1
optimization ×1
optional ×1
refactoring ×1
swing ×1