私はすべてのものをよりきれいに見えるようにするためにコードを減らそうとしていますが、このコードを変更する方法はわかりませんので、コードは少なくて済みます。誰もがこのコードを同じ結果で減らすことはできますか?
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]に適しています。 –