parseInt行中有错误

pingboo23

因此,我正在使用我的这个Android数学游戏。而且我很难弄清楚为什么我得到了这些错误并尝试了一些在互联网上找到的代码,但是它不起作用。

我在下面的代码没有错误。但是,当我尝试运行它时,我的logcat中出现错误。

 check.setOnClickListener(new OnClickListener(){

        int x = Integer.valueOf(fn.getText().toString());
        int y = Integer.valueOf(sn.getText().toString());

        public void onClick(View v){
         String ope = op.getText().toString();


         if(ope=="+"){
             if(x + y == total2){
                 Toast.makeText(getApplicationContext(), "Answer is correct. You may proceed to level 2.", Toast.LENGTH_LONG).show();
             }
         }

         if(ope=="-"){
             if(x-y==total2){
                 Toast.makeText(getApplicationContext(), "Answer is correct. You may proceed to level 2.", Toast.LENGTH_LONG).show();
             }
         }  


         if(ope=="*"){
             if(x*y==total2){
                 Toast.makeText(getApplicationContext(), "Answer is correct. You may proceed to level 2.", Toast.LENGTH_LONG).show();
             }
         }

         if(ope=="/"){
             if(x/y==total2){
                 Toast.makeText(getApplicationContext(), "Answer is correct. You may proceed to level 2.", Toast.LENGTH_LONG).show();
             }
             else if(y/x==total2){
                 Toast.makeText(getApplicationContext(), "Answer is correct. You may proceed to level 2.", Toast.LENGTH_LONG).show();
             }

         }


         }});

这是我的LOGCAT:

在此处输入图片说明

我正在解析正确,对不对?那么,为什么我有这些错误?

注意: fn和sn是textViews,而op也是。fn和sn是用户放置其操作数答案的位置,而op是运算符。游戏具有给定的随机数,用户应轻按2个操作数和一个运算符来建立方程式,并能够得出给定的随机数。她/他的方程式的结果应与给定的随机数相同。

谢谢 :)

朱尼德·阿桑

您的代码中有两个问题。首先,正如亨利在回答中提到的那样,您需要放置以下行:

  int x = Integer.valueOf(fn.getText().toString());
  int y = Integer.valueOf(sn.getText().toString());

onClick方法内部

第二个问题是您正在使用==所有if检查来进行字符串比较,例如:

     if(ope=="+")

您应该使用Stringequals()方法进行字符串比较。更改它和其他条件以使用equals方法,如下所示:

     if(ope.equals("+"))

==比较两个引用是否指向相同的内存位置,同时equals()比较字符串内容。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章