我收到错误:
error: bad operand types for binary operator '*'
if ((monthD * dayD).equals("yearD"))
^
first type: String
second type: String
代码的要点是查看月份和日期是否等于乘以的年份。
这是我的代码:
// Month
String monthD;
System.out.print("What is the month? (For example, August would inserted as '08') ");
monthD = userInput.nextLine();
// Day
String dayD;
System.out.print("What is the day? (For example, the 10th of the month is inserted as '10') ");
dayD = userInput.nextLine();
// Year
String yearD;
System.out.print("What is the year? (For example, 1998 is inserted as '98') ");
yearD = userInput.nextLine();
// Is this date magic?
if ((monthD * dayD).equals("yearD"))
{
System.out.print("This date is magic!");
}
else
{
System.out.print("This date is not magic!");
}
答案 0 :(得分:0)
你不能拥有String
数据类型' *' (乘法)目的。
而是使用int
类型。
答案 1 :(得分:0)
您需要使用Integer.parseInt(String)
将字符串转换为整数。用try / catch环绕以检查不是真正的整数。并且你甚至想要做什么,只需使用java.util.Date中的new Date()
来获取日期