等号函数在我的java代码中不起作用

时间:2016-06-15 09:17:32

标签: java android eclipse equals equals-operator

我的xml中有2个textviews,1个editbox和1个按钮。

在我的代码中,这一行无效:

if(c.equals(str)){
                Toast.makeText(GameActivity.this, "Alright",Toast.LENGTH_LONG).show();
            }

但是下一行(else)正在工作,在这两种情况下(如果& else)显示else函数toast(“Wrong”)。

我的代码是:

public class GameActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game);

    Button b1 = (Button) findViewById(R.id.button1);
    b1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) { 
            TextView tv = (TextView) findViewById(R.id.textView1);
            TextView tv2 = (TextView) findViewById(R.id.textView2);
            Random r = new Random();
            int i = r.nextInt(10 - 2) + 2;
            tv.setText(i +"");
            Random r1 = new Random();
            int j = r1.nextInt((9 - 2) + 1) + 2;
            tv2.setText(j +"");

            int a = Integer.parseInt(tv.getText().toString());
            int b = Integer.parseInt(tv2.getText().toString());
            int result =  a*b;
            String str = String.valueOf(result);

            EditText txt4 = (EditText) findViewById(R.id.editText1);
            String c = txt4.getText().toString();

            if(TextUtils.isEmpty(txt4.getText().toString())) {
                txt4.setError("Please enter your answer");
                return;
            }

            if(c.equals(str)){
                Toast.makeText(GameActivity.this, "Alright",Toast.LENGTH_LONG).show();

            }

            else{
                Toast.makeText(GameActivity.this, "wrong",Toast.LENGTH_LONG).show();
            }

            txt4.setText("");
        }

    });
    }

2 个答案:

答案 0 :(得分:1)

而不是

String str = String.valueOf(result);

你应该使用

String str = Integer.toString(result);

答案 1 :(得分:1)

试试这个, 我得到了结果

public class Test extends AppCompatActivity {
Button b1;
TextView t1,t2;
EditText e1;
int a,b;
@Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
     b1 = (Button) findViewById(R.id.button2);
     t1=(TextView) findViewById(R.id.textView4);
     t2=(TextView) findViewById(R.id.textView5);
     e1=(EditText) findViewById(R.id.editText);
    Random r = new Random();
    int i = r.nextInt(10 - 2) + 2;
    t1.setText(i +"");
    Random r1 = new Random();
    int j = r1.nextInt((9 - 2) + 1) + 2;
    t2.setText(j +"");

     a = Integer.parseInt(t1.getText().toString());
     b = Integer.parseInt(t2.getText().toString());

  b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            int result =  a*b;
            String str = String.valueOf(result);

            String c = e1.getText().toString();

            if(TextUtils.isEmpty(c)) {
                e1.setError("Please enter your answer");
                return;
            }

            if(c.equals(str)){
                Toast.makeText(Test.this, "Alright",Toast.LENGTH_LONG).show();

            }

            else{
                Toast.makeText(Test.this, "wrong",Toast.LENGTH_LONG).show();
            }

            e1.setText("");
        }

    });
}}