我正在尝试减少代码以使我的一切看起来更干净和漂亮但我不知道如何更改此代码以使函数保持相同但代码更少。我也是java的新手,所以如果有其他方法让我用相同的输出编写这个代码我会很感激,如果有人能告诉我如何
uncaught exception: undefined
答案 0 :(得分:0)
将代码拆分为单独的方法并使用switch case(如果java> = 7):
static class Action4 implements ActionListener {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
String name = ((JTextField) e.getSource()).getText();
switch(name) {
case "test1":
process(20);
break;
case "test2":
process(17);
break;
case "test3":
process(22);
break;
default: JOptionPane.showMessageDialog(null, "Wrong input!");
}
}
public function getInput(int factor) {
name = JOptionPane.showInputDialog("Enter Name ");
String day;
int totalCost;
int visitors;
day = JOptionPane.showInputDialog("Enter what day you'd like to attend ");
visitors = Integer.parseInt(JOptionPane.showInputDialog("Enter how many people are visiting "));
totalCost = visitors * factor;
JOptionPane.showMessageDialog(null, " You are attending the " + name + " On " + day + visitors + " attending " + "total cost " + totalCost);
}
}
答案 1 :(得分:0)
class Action4 implements ActionListener {
String name = null;
String day;
int totalCost;
int visitors;
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
name = ((JTextField) e.getSource()).getText();
if (name.equals("Test1")) {
init(20);
} else if (name.equals("test2")) {
init(17);
} else if (name.equals("test3")) {
init(22);
} else {
JOptionPane.showMessageDialog(null, "Wrong input!");
}
}
private void init(int value) {
name = JOptionPane.showInputDialog("Enter Name ");
day = JOptionPane.showInputDialog("Enter what day you'd like to attend ");
visitors = Integer.parseInt(JOptionPane.showInputDialog("Enter how many people are visiting "));
totalCost = visitors * value;
JOptionPane.showMessageDialog(null, " You are attending the " + name + " On " + day + visitors + " attending " + "total cost " + totalCost);
}
}
答案 2 :(得分:0)
希望这可以作为你的工作
static class Action4 implements ActionListener {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
String name = ((JTextField) e.getSource()).getText();
name = JOptionPane.showInputDialog("Enter Name ");
String day;
int totalCost;
int visitors;
int multiplier = 0;
day = JOptionPane.showInputDialog("Enter what day you'd like to attend ");
visitors = Integer.parseInt(JOptionPane.showInputDialog("Enter how many people are visiting "));
if (name.equals("Test1"))
multiplier = 20;
else if (name.equals("test2"))
multiplier = 17;
else if (name.equals("test3"))
multiplier = 22;
else
JOptionPane.showMessageDialog(null, "Wrong input!");
totalCost = visitors * multiplier;
if(multiplier != 0)
JOptionPane.showMessageDialog(null, " You are attending the " + name + " On " + day + visitors + " attending " + "total cost " + totalCost);
}
答案 3 :(得分:0)
我建议您每当有一系列选项时考虑使用enum
。这允许整理器封装,并且更容易添加新条目而无需更改任何其他代码。
public enum Test {
TEST1("test1", 20),
TEST2("test2", 17),
TEST3("test3", 22);
private final String name;
private final int costPerVisitor;
private Test(String name, int costPerVisitor) {
this.name = name;
this.costPerVisitor = costPerVisitor;
}
public static Optional<Test> getTestWithName(String name) {
for (Test test: values()) {
if (test.name.equals(name))
return Optional.of(test);
}
return Optional.empty();
}
public int getTotalCost(int visitors) {
return visitors * costPerVisitor;
}
}
这可以用于:
Optional<Test> possibleTest = Test.getTestWithName(name);
if (possibleTest.isPresent()) {
...
int totalCost = possibleTest.get().getTotalCost(visitor);
} else {
showMessageDialog(null, "Wrong input!");
}
它使用Optional
,但您可以轻松(尽管不太清晰)使用null
表示&#34;没有使用该名称进行测试&#34;。
请注意,在您的情况下,您可以完全避开name
字段并使用name().toLower()
。