这是什么原因造成的;不兼容的操作数类型int和java.lang.String“;
我在if行上得到了不兼容的操作数类型int和java.lang.String。我不知道它在问什么,也不知道如何修复它这是什么原因造成的;不兼容的操作数类型int和java.lang.String“;,java,compiler-errors,Java,Compiler Errors,我在if行上得到了不兼容的操作数类型int和java.lang.String。我不知道它在问什么,也不知道如何修复它 import java.io.*; import static java.lang.System.*; //不确定是否需要上述导入 导入java.util.Scanner class Main{ public static void main (String str[]) throws IOException { Scanner scan =
import java.io.*;
import static java.lang.System.*;
//不确定是否需要上述导入
导入java.util.Scanner
class Main{
public static void main (String str[]) throws IOException {
Scanner scan = new Scanner(System.in);
String ap = "April";
String mar = "March";
int one = 1;
int two = 2;
System.out.println("What month were you born in? (number)");
int month = scan.nextInt();
System.out.println("What day (number)");
int day = scan.nextInt();
if(((month == ap) && (day <= 19) || (month == mar) && (day >= 21))){
System.out.println("Your birthday is: "+ month+ " "+day);
System.out.println("Aries");
System.out.println("Horoscope: ");
}else
System.out.println("HI"); //Just some filler code for compiling
}
}
主类{
公共静态void main(字符串str[])引发IOException{
扫描仪扫描=新扫描仪(System.in);
字符串ap=“四月”;
字符串mar=“三月”;
int-one=1;
int 2=2;
System.out.println(“你出生在哪个月?(数字)”);
int month=scan.nextInt();
System.out.println(“星期几(数字)”);
int day=scan.nextInt();
如果((月=ap)和&(日=21))){
System.out.println(“您的生日是:“+月+”+日);
System.out.println(“Aries”);
System.out.println(“星座:”);
}否则
System.out.println(“HI”);//只是一些用于编译的填充代码
}
}
您不能直接比较字符串
和int
(您可以获得字符串
int的值,但我不认为这对您有帮助),而且在您的用例中唯一对我有意义的导入静态是java.util.Calendar.
。然后需要从输入的月份中减去一,因为Java将(第一个月和该月的活动)视为月份0
。最后,当您的输入不在预期范围内时,请显示有意义的内容,而不是“嗨”。差不多
import java.io.IOException;
import java.util.Scanner;
import static java.util.Calendar.*;
class Main {
public static void main(String str[]) throws IOException {
Scanner scan = new Scanner(System.in);
System.out.println("What month were you born in? (number)");
int month = scan.nextInt() - 1;
System.out.println("What day (number)");
int day = scan.nextInt();
if (((month == APRIL) && (day <= 19) ||
(month == MARCH) && (day >= 21))) {
System.out.println("Your birthday is: " + month + " " + day);
System.out.println("Aries");
System.out.println("Horoscope: ");
} else {
System.out.printf("Month %d, Day %d%n", month, day);
}
}
}
import java.io.IOException;
导入java.util.Scanner;
导入静态java.util.Calendar.*;
班长{
公共静态void main(字符串str[])引发IOException{
扫描仪扫描=新扫描仪(System.in);
System.out.println(“你出生在哪个月?(数字)”);
int month=scan.nextInt()-1;
System.out.println(“星期几(数字)”);
int day=scan.nextInt();
如果((月=4月)和&(日=21))){
System.out.println(“您的生日是:“+月+”+日);
System.out.println(“Aries”);
System.out.println(“星座:”);
}否则{
System.out.printf(“月%d,日%d%n”,月,日);
}
}
}
一个数字n
(如1或2)如何成为字符串
如“April”?您尝试比较字符串和整数,这是不可能的。您对月==mar
或等效值进行比较的目的是什么:例如5==March
?即使编译,也永远不会匹配。您应该以文本形式阅读月份(并与equals
/equalsIgnorecase
进行比较),或者在变量中使用月份的数字,而不是它们的英文名称。