내 모든 것이 더 깨끗하고 멋지게 보이도록 코드를 줄이려고하지만이 코드를 변경하는 방법을 모르므로 기능은 동일하지만 코드는 적게 유지됩니다. 사람이이 코드를 동일한 결과로 줄일 수 있습니까?
static class Action4 implements ActionListener {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
String name = ((JTextField) e.getSource()).getText();
if (name.equals("Test1")) {
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 * 20;
JOptionPane.showMessageDialog(null, " You are attending the " + name + " On " + day + visitors + " attending " + "total cost " + totalCost);
} else {
if (name.equals("test2")) {
name = JOptionPane.showInputDialog("Enter Name ");
String day;
day = JOptionPane.showInputDialog("Enter what day you'd like to attend ");
int visitors;
visitors = Integer.parseInt(JOptionPane.showInputDialog("Enter how many people are visiting "));
int totalCost;
totalCost = visitors * 17;
JOptionPane.showMessageDialog(null, " You are attending the " + name + " On " + day + visitors + " attending " + "total cost " + totalCost);
} else {
if (name.equals("test3")) {
name = JOptionPane.showInputDialog("Enter Name ");
String day;
day = JOptionPane.showInputDialog("Enter what day you'd like to attend ");
int visitors;
visitors = Integer.parseInt(JOptionPane.showInputDialog("Enter how many people are visiting "));
int totalCost;
totalCost = visitors * 22;
JOptionPane.showMessageDialog(null, " You are attending the " + name + " On " + day + visitors + " attending " + "total cost " + totalCost);
} else {
JOptionPane.showMessageDialog(null, "Wrong input!");
}
}
가장 확실한 변경 : 'else {if'가 아니라'else if'를 사용하십시오. –
이 질문은 [codereview.se]에 더 적합합니다. –