有没有办法将这两个 if 语句结合起来?
Is there a way to combine these two if statements?
我正在处理一个个人任务,用户输入他们的生日,然后它会显示用户的星座和星座描述。但是由于所有的星座都是多个月,所以我将 if 语句拆分为多个 if 语句,每个月一个,例如
if (birthMonth == 5 && (21<=birthDay && birthDay <=31)) {
JFrame frame = new JFrame();
String symbol = "Your sign is Gemini. You are cerebral, chatty, love learning and education, charming, and adventurous. ";
symbol += bday;
JLabel label = new JLabel(symbol, new ImageIcon("gemini.jpg"), JLabel.CENTER);
label.setVerticalTextPosition(SwingConstants.TOP);
frame.add(label);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
else if (birthMonth == 6 && (1<=birthDay && birthDay <=21)) {
JFrame frame = new JFrame();
String symbol = "Your sign is Gemini. You are cerebral, chatty, love learning and education, charming, and adventurous. ";
symbol += bday;
JLabel label = new JLabel(symbol, new ImageIcon("gemini.jpg"), JLabel.CENTER);
label.setVerticalTextPosition(SwingConstants.TOP);
frame.add(label);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
我想知道是否有一种方法可以组合 if 语句,同时保持代码按当前方式工作。非常感谢!
逻辑和行为都是相同的,因此您不必使用两个条件来编写您的逻辑,您可以将下面的代码作为一种方式使用。
if (birthMonth == 5 && (21<=birthDay && birthDay <=31) || birthMonth == 6 && (1<=birthDay && birthDay <=21)) {
JFrame frame = new JFrame();
String symbol = "Your sign is Gemini. You are cerebral, chatty, love learning and education, charming, and adventurous. ";
symbol += bday;
JLabel label = new JLabel(symbol, new ImageIcon("gemini.jpg"), JLabel.CENTER);
label.setVerticalTextPosition(SwingConstants.TOP);
frame.add(label);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
为什么不使用 or 运算符 || ?像这样:
(birthMonth == 5 && (21<=birthDay && birthDay <=31) || (birthMonth == 6 & (1<=birthDay && birthDay <=21)))
我正在处理一个个人任务,用户输入他们的生日,然后它会显示用户的星座和星座描述。但是由于所有的星座都是多个月,所以我将 if 语句拆分为多个 if 语句,每个月一个,例如
if (birthMonth == 5 && (21<=birthDay && birthDay <=31)) {
JFrame frame = new JFrame();
String symbol = "Your sign is Gemini. You are cerebral, chatty, love learning and education, charming, and adventurous. ";
symbol += bday;
JLabel label = new JLabel(symbol, new ImageIcon("gemini.jpg"), JLabel.CENTER);
label.setVerticalTextPosition(SwingConstants.TOP);
frame.add(label);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
else if (birthMonth == 6 && (1<=birthDay && birthDay <=21)) {
JFrame frame = new JFrame();
String symbol = "Your sign is Gemini. You are cerebral, chatty, love learning and education, charming, and adventurous. ";
symbol += bday;
JLabel label = new JLabel(symbol, new ImageIcon("gemini.jpg"), JLabel.CENTER);
label.setVerticalTextPosition(SwingConstants.TOP);
frame.add(label);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
我想知道是否有一种方法可以组合 if 语句,同时保持代码按当前方式工作。非常感谢!
逻辑和行为都是相同的,因此您不必使用两个条件来编写您的逻辑,您可以将下面的代码作为一种方式使用。
if (birthMonth == 5 && (21<=birthDay && birthDay <=31) || birthMonth == 6 && (1<=birthDay && birthDay <=21)) {
JFrame frame = new JFrame();
String symbol = "Your sign is Gemini. You are cerebral, chatty, love learning and education, charming, and adventurous. ";
symbol += bday;
JLabel label = new JLabel(symbol, new ImageIcon("gemini.jpg"), JLabel.CENTER);
label.setVerticalTextPosition(SwingConstants.TOP);
frame.add(label);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
为什么不使用 or 运算符 || ?像这样:
(birthMonth == 5 && (21<=birthDay && birthDay <=31) || (birthMonth == 6 & (1<=birthDay && birthDay <=21)))