返回的字符串JOptionPane.showInputDialog()
与常规字符串不同吗?当我尝试将其与 进行比较时"2"
,它返回 false 并转到 else 块。
// Prompt user for the month
String monthString = JOptionPane.showInputDialog(
"Enter which month are you looking for: ");
// SMALL Months, ie: 2, 4, 6, 9, 11
else {
// Special case on February
if (monthString == "2" && isLeap)
result += "29 days!";
else if (monthString == "2")
result += "28 days!";
// Everytime my code to go to this block instead
else
result += "30 days!";
}
仅当我将月份解析为 Int 然后将其与文字 2 进行比较时才有效。为什么我的原始版本不起作用?
int month = Integer.parseInt(monthString);
if (month == 2 && isLeap) ...