【问题标题】:bad operand types for binary operator '<' first type: float second type: Object二元运算符“<”的错误操作数类型第一种类型:浮点第二种类型:对象
【发布时间】:2014-11-11 11:08:49
【问题描述】:

我想编写一个简单的 Java 程序,但出现以下错误:

bad operand types for binary operator '<'
  first type:  float
  second type: Object

这是我的代码:

public static void main(String[] args) {

    ArrayList pa = new ArrayList();
    pa.add(100);
    pa.add(85);
    pa.add(80);
    pa.add(75);
    pa.add(70);
    pa.add(60);
    pa.add(50);
    pa.add(40);

    int [] pb = new int[8]; 
    pb[0] =85;
    pb[1] =80;
    pb[2] =75;
    pb[3] =70;
    pb[4] =60;
    pb[5] =50;
    pb[6] =40;
    pb[7] =30;

    float input ;
    string grade;

        if ( (input < pa.get(1) ) && (input270 >= pb270[0]) ) // this is the problem
            {   grade = "A+";   
        } 

【问题讨论】:

  • 错误信息很清楚,不是吗?
  • 是的,谢谢你的帮助

标签: java


【解决方案1】:

变化:

ArrayList pa = new ArrayList();

到:

ArrayList<Integer> pa = new ArrayList<>();

或者如果您愿意:

ArrayList<Float> pa = new ArrayList<>();

如果不告诉它列表中数据的类型,它会将所有元素视为Object,并且您无法使用&lt; 运算符将floatObject 进行比较。

【讨论】:

    【解决方案2】:

    定义您的集合,使其具有类型信息,而不是使用raw type

    List<Integer> pa = new ArrayList<>();
    

    【讨论】:

      猜你喜欢
      • 2013-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多