是什么原因造成的 "Incompatible operand types int and java.lang.String"
What causes this "Incompatible operand types int and java.lang.String"
我在我的 if 行上得到 Incompatible operand types 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 = 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
}
}
您不能直接比较 String
和 int
(您可以获得 int
的 String
值,但我认为这对您没有帮助在这里),在你的用例中唯一对我有意义的导入静态是 java.util.Calendar.*
。然后您需要从输入的月份中减去一个,因为 Java 将 Calendar.JANUARY
(the first month and what the Static Import 视为月份 0
。最后,当您的输入不在预期范围内时,请显示一些有意义的内容而不是 "HI"。像
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);
}
}
}
我在我的 if 行上得到 Incompatible operand types 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 = 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
}
}
您不能直接比较 String
和 int
(您可以获得 int
的 String
值,但我认为这对您没有帮助在这里),在你的用例中唯一对我有意义的导入静态是 java.util.Calendar.*
。然后您需要从输入的月份中减去一个,因为 Java 将 Calendar.JANUARY
(the first month and what the Static Import 视为月份 0
。最后,当您的输入不在预期范围内时,请显示一些有意义的内容而不是 "HI"。像
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);
}
}
}