jeudi 18 juin 2015

How to Randomly Choose Button Text in Java?

For example i have 3 buttons and 3 int values...

how to make buttons text randomly choose from 3 int values string while button click....

its a Simple maths game for android Platforms..

For example i have 3 buttons and 3 int values...

how to make buttons text randomly choose from 3 int values string while button click....

its a Simple maths game for android Platforms..

        TextView textA = (TextView) findViewById(R.id.textView);
        TextView textB = (TextView) findViewById(R.id.textView3);
        final Button button1 = (Button) findViewById(R.id.button1);
        final Button button2 = (Button) findViewById(R.id.button2);
        final Button button3 = (Button) findViewById(R.id.button3);


        Random rand = new Random();
        final int x = rand.nextInt(1000);
        Random randB = new Random();
        int y = randB.nextInt(1000);
        Random randw = new Random();
        int w = randw.nextInt(1000);
        Random randz = new Random();
        int z = randz.nextInt(1000);

        int A = x;
        int B = y;
        int Wrong1 = (A + w);
        int Wrong2 = (A + z);
        final int CorrectAnswer = (A + B);

        String text = "text";
        Button[] arr = {button1, button2, button3};
        Random r = new Random();
        Button b = arr[r.nextInt(arr.length)];
        b.setText(text);

        int Ans1 = CorrectAnswer;
        int Ans2 = Wrong1;
        int Ans3 = Wrong2;


        textA.setText("" + A);
        textB.setText("" + B);
        button1.setText("" + Wrong1);
        button2.setText("" + Wrong2);
        button3.setText("" + CorrectAnswer);
        final int ACAns;
        ACAns = Integer.parseInt(button3.getText().toString());
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int AAns = Integer.parseInt(button1.getText().toString());


                if ((AAns == ACAns)) {
                    Toast.makeText(getBaseContext(), "Your answer is correct!", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getBaseContext(), "Your answer is Wrong!", Toast.LENGTH_SHORT).show();
                }

            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int BAns = Integer.parseInt(button2.getText().toString());
                int ACAns = Integer.parseInt(button1.getText().toString());
                if ((BAns == ACAns)) {
                    Toast.makeText(getBaseContext(), "Your answer is correct!", Toast.LENGTH_SHORT).show();
                } else {
                    }    
            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int CAns = Integer.parseInt(button3.getText().toString());
                int ACAns = Integer.parseInt(button3.getText().toString());
                if ((CAns == ACAns)) {
                    Toast.makeText(getBaseContext(), "Your answer is correct!", Toast.LENGTH_SHORT).show();                      

                    return;
                } else {
                    Toast.makeText(getBaseContext(), "Your answer is Wrong!", Toast.LENGTH_SHORT).show();
                }




Aucun commentaire:

Enregistrer un commentaire