导致这种“不兼容的操作数类型int和java.lang.String”的原因

时间:2015-04-04 19:59:48

标签: java compiler-errors

我在if行上获得了不兼容的操作数类型int和java.lang.String。我不知道它在问什么或如何解决它。

import java.io.*;
import static java.lang.System.*;

//不确定是否需要上面的导入     import 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 



     }

}

1 个答案:

答案 0 :(得分:2)

您无法直接比较Stringint(您可以获得String的{​​{1}}值,但我认为这不会对您有所帮助)在你的用例中,唯一对我有意义的导入静态是int。然后,您需要从输入月中减去一个,因为Java将Calendar.JANUARY(第一个月和Static Import所做的)视为月java.util.Calendar.*。最后,当您的输入不在预期范围内时,请显示有意义的内容而不是“HI”。像

这样的东西
0