什么是android代码错误?

时间:2014-07-07 09:56:23

标签: java android

android代码有什么问题?我希望这段代码在

时给出错误

输入的答案是错误的,并且在答案正确但每次都是正确的   得到不正确

并且我的投射变量是正确的

public class MainActivity extends Activity {

    TextView Jlabel1;
    TextView Jlabel2;
    TextView Jlabel3;
    EditText Jtextbox1;
    Button b1;
    int m;
    int ans;
    String ans1;
    String k;

    @Override


        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Jlabel1 = (TextView) findViewById(R.id.textView1);
        Jlabel2 = (TextView) findViewById(R.id.textView2);
        Jlabel3 = (TextView) findViewById(R.id.textView3);
        Jtextbox1 = (EditText) findViewById(R.id.editText1);
        b1 = (Button) findViewById(R.id.button1);

        double q = Math.random();
        double w = Math.random();
        int e = (int) (q * 10);

        int z = (int) (w * 10);
        ans = e + z;

        Jlabel1.setText(Integer.toString(e));
        Jlabel2.setText(Integer.toString(z));

        ans1 = String.valueOf(ans);


        k = Jtextbox1.getText().toString();


        b1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ans1.equals(k)) {
                    Jlabel3.setText("Correct");
                } else {
                    Jlabel3.setText("Incorrect");
                }

            }
        });
    } 
}'

1 个答案:

答案 0 :(得分:1)

您过早收到edittext内容的副本。移动

k = Jtextbox1.getText().toString();

onClick()内。