Java 二进制运算符'*';
我得到了一个错误:Java 二进制运算符'*';,java,Java,我得到了一个错误: 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? (F
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!");
}
不能将
String
数据类型用于“*”(乘法)目的。
改为使用
int
类型。您需要使用Integer.parseInt(String)
将字符串转换为整数。用try/catch环绕以检查非实整数。你到底想做什么,只需使用java.util.Date中的new Date()
来获取日期为什么要在需要int时将输入读取为字符串?你的逻辑也是错误的,首先检查你想要比较的内容。我尝试了int和double,但都不起作用,这只是我尝试的最后一个选项。我还应该做什么呢?我已经尝试了这么多不同的变化,字符串只是我最后一次尝试。我可以使用什么?使用'int'并在'if'循环中,删除“.euals(yearD)”周围的双引号,或者简单地使用“if(monthD*dayD==yearD){…你的代码…}或者{…你的代码…}”我很抱歉,我在我的类中没有走那么远,我认为它不必那么复杂,恕我冒犯。我只想看看这个月x天=年