我正试着为一些椭圆形做颜色。用户键入例如颜色红色(十字形),然后圆圈将被绘制并涂成红色,这是有效的,但现在我试图让它给出一个弹出窗口,如果颜色不匹配那些用户应该给出的,或者如果用户给出了一个例如,那么用户可以填写的三种颜色是 rood (红色), groen (绿色)和 oranje (橙色)。
有人告诉我,我应该尝试使用try和catch方法,我在下面尝试过,当用户输入例如rood(红色)时,圆圈仍然可以工作并且会被绘制成红色,但是当他放弃时例如蓝色,没有任何反应;我希望它然后给出一条错误消息,说用户需要提供三种列出的颜色,而不是其他颜色。
this.kleur = this.jTextFieldKleur.getText();
repaint();
if (kleur != null) {
try {
if (kleur.equals("rood")) {
g.setColor(Color.RED);
g.fillOval(75, 60, 35, 35); //links boven
g.fillOval(130, 60, 35, 35); //rechts boven
g.fillOval(75, 130, 35, 35); //links onder
g.fillOval(130, 130, 35, 35); //links onder
} else if (kleur.equals("groen")) {
g.setColor(Color.GREEN);
g.fillOval(75, 60, 35, 35); //links boven
g.fillOval(130, 60, 35, 35); //rechts boven
g.fillOval(75, 130, 35, 35); //links onder
g.fillOval(130, 130, 35, 35); //links onder
} else if (kleur.equals("oranje")) {
g.setColor(Color.ORANGE);
g.fillOval(75, 60, 35, 35); //links boven
g.fillOval(130, 60, 35, 35); //rechts boven
g.fillOval(75, 130, 35, 35); //links onder
g.fillOval(130, 130, 35, 35); //links onder
}
} catch (Exception e) {
{
JOptionPane.showMessageDialog(null, "Gelieve te kiezen uit rood, oranje of groen.");
}
}
}
答案 0 :(得分:2)
这里没有抛出异常,因此catch
子句毫无意义。相反,您可以在else
块中使用此逻辑。请注意,检查kleur
null
值不应该是特殊情况。你可以通过使用yoda-notations来处理这个问题,或者更好的是,使用交换机案例:
switch (kleur) {
case "rood":
g.setColor(Color.RED);
g.fillOval(75, 60, 35, 35); //links boven
g.fillOval(130, 60, 35, 35); //rechts boven
g.fillOval(75, 130, 35, 35); //links onder
g.fillOval(130, 130, 35, 35); //links onder
break;
case "groen":
g.setColor(Color.GREEN);
g.fillOval(75, 60, 35, 35); //links boven
g.fillOval(130, 60, 35, 35); //rechts boven
g.fillOval(75, 130, 35, 35); //links onder
g.fillOval(130, 130, 35, 35); //links onder
break;
case "oranje":
g.setColor(Color.ORANGE);
g.fillOval(75, 60, 35, 35); //links boven
g.fillOval(130, 60, 35, 35); //rechts boven
g.fillOval(75, 130, 35, 35); //links onder
g.fillOval(130, 130, 35, 35); //links onder
break;
default:
JOptionPane.showMessageDialog
(null, "Gelieve te kiezen uit rood, oranje of groen.");
}
答案 1 :(得分:2)
告诉你的人......给了你错误的输入。如果你的代码可能抛出异常,你只需要try / catch。
在您的情况下,您需要将该错误消息放在所有ifs失败时使用的“路径”中;例如,通过使用switch语句;最后一个案例是默认;这会给你一个错误。非常像Mureink给你的好答案。
但真正的要点是:不要相信其他新手告诉你的内容。相反:当使用诸如try / catch之类的概念时学习那件事是什么。永远不要因为有人告诉你这样做而使用某些东西 - 总是理解你在做什么。
从这个意义上讲:在这里查看switch,然后查看exceptions/try/catch。
答案 2 :(得分:1)
您在这里混淆了try..catch
和if..else
。当没有选项匹配时,if..else
不会抛出异常。
请改为尝试:
if (kleur != null) {
if (kleur.equals("rood")) {
g.setColor(Color.RED);
g.fillOval(75, 60, 35, 35); //links boven
g.fillOval(130, 60, 35, 35); //rechts boven
g.fillOval(75, 130, 35, 35); //links onder
g.fillOval(130, 130, 35, 35); //links onder
} else if (kleur.equals("groen")) {
g.setColor(Color.GREEN);
g.fillOval(75, 60, 35, 35); //links boven
g.fillOval(130, 60, 35, 35); //rechts boven
g.fillOval(75, 130, 35, 35); //links onder
g.fillOval(130, 130, 35, 35); //links onder
} else if (kleur.equals("oranje")) {
g.setColor(Color.ORANGE);
g.fillOval(75, 60, 35, 35); //links boven
g.fillOval(130, 60, 35, 35); //rechts boven
g.fillOval(75, 130, 35, 35); //links onder
g.fillOval(130, 130, 35, 35); //links onder
} else {
JOptionPane.showMessageDialog(null, "Gelieve te kiezen uit rood, oranje of groen.");
}
}
稍后重构的版本通过调用常量字符串上的equals
方法来检查null:
if ("rood".equals(kleur)) {
g.setColor(Color.RED);
g.fillOval(75, 60, 35, 35); //links boven
g.fillOval(130, 60, 35, 35); //rechts boven
g.fillOval(75, 130, 35, 35); //links onder
g.fillOval(130, 130, 35, 35); //links onder
} else if ("groen".equals(kleur)) {
g.setColor(Color.GREEN);
g.fillOval(75, 60, 35, 35); //links boven
g.fillOval(130, 60, 35, 35); //rechts boven
g.fillOval(75, 130, 35, 35); //links onder
g.fillOval(130, 130, 35, 35); //links onder
} else if ("oranje".equals(kleur)) {
g.setColor(Color.ORANGE);
g.fillOval(75, 60, 35, 35); //links boven
g.fillOval(130, 60, 35, 35); //rechts boven
g.fillOval(75, 130, 35, 35); //links onder
g.fillOval(130, 130, 35, 35); //links onder
} else {
JOptionPane.showMessageDialog(null, "Gelieve te kiezen uit rood, oranje of groen.");
}
考虑到一些书籍建议尽快从方法返回,从而使代码更容易理解:
if ("rood".equals(kleur)) {
g.setColor(Color.RED);
g.fillOval(75, 60, 35, 35); //links boven
g.fillOval(130, 60, 35, 35); //rechts boven
g.fillOval(75, 130, 35, 35); //links onder
g.fillOval(130, 130, 35, 35); //links onder
return:
}
if ("groen".equals(kleur)) {
g.setColor(Color.GREEN);
g.fillOval(75, 60, 35, 35); //links boven
g.fillOval(130, 60, 35, 35); //rechts boven
g.fillOval(75, 130, 35, 35); //links onder
g.fillOval(130, 130, 35, 35); //links onder
return;
}
if ("oranje".equals(kleur)) {
g.setColor(Color.ORANGE);
g.fillOval(75, 60, 35, 35); //links boven
g.fillOval(130, 60, 35, 35); //rechts boven
g.fillOval(75, 130, 35, 35); //links onder
g.fillOval(130, 130, 35, 35); //links onder
return;
}
JOptionPane.showMessageDialog(null, "Gelieve te kiezen uit rood, oranje of groen.");
return;
最后一个代码段暗示您的方法只会用颜色填充椭圆。这种限制实际上是一件好事,因为你应该尽可能地保持你的方法。
您可以清楚地看到您的代码包含大量重复内容。我们可以解决这个问题,并提高可扩展性。这里有一些实验性的未经测试的代码:
private static final Map<String, Color> colorsNL = new HashMap<>();
static {
colorsNL.put("rood", Color.RED);
colorsNL.put("groen", Color.GREEN);
colorsNL.put("oranje", Color.ORANGE);
}
private Shape g = ...
private void setColor(String colorNL) {
if (!this.colorsNL.contains(colorNL)) {
JOptionPane.showMessageDialog(null, "Gelieve te kiezen uit rood, oranje of groen.");
return;
}
this.g.setColor(this.colorsNL.get(colorNL));
this.fillOvals();
}
private void fillOvals() {
g.fillOval(75, 60, 35, 35); //links boven
g.fillOval(130, 60, 35, 35); //rechts boven
g.fillOval(75, 130, 35, 35); //links onder
g.fillOval(130, 130, 35, 35); //links onder
}
答案 3 :(得分:0)
不确定我是否理解正确,但你不想做这样的事吗?
let errors = [{
field: "data.time",
message: "less length than allowed"
}, {
field: "data.time",
message: "wrong format"
}, {
field: "data.age",
message: "too young"
}, {
field: "data.age",
message: "not enough information"
}];
var mappedData = errors.reduce((result, currentValue) => {
let type = currentValue.field.split(/\./).pop(),
message = currentValue.message.replace(/\s/g, "_")
if (typeof result[type] === "undefined") {
result[type] = message
} else {
result[type] += " <br> " + message;
}
return result;
}, {});
console.log(mappedData);