mardi 2 octobre 2018

Distinct random numbers

So the issue is as follows I managed to make a small program where random images appeared and 3 multiple choice questions are under as a Radio group. the picture is set randomly from a pool of 6 images and the 3 answers are brought from a string array containing all the names of the pictures. now the first radio button is the one that always has the correct answer because it has the same value as the int that sets the picture while the other two are random from the set of string values that I have specified.

The two problems are

One

Sometimes I would get the same answer in two or three radio buttons which is an issue.so I want to have it so that the answer cannot be repeated.

Two

the fact that the first answer is always the correct answer is problematic so how can I randomize the position of it and have the check button always be able to know what the correct answer is.

//below this is the entire java code

package com.example.hamdanali.imageview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import java.util.LinkedHashSet;

public class imagegame extends AppCompatActivity {

ImageView pic ;
Button    btn1;
Button    btn2;
Button    btn3;
Button    check;


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

    pic =   findViewById(R.id.Pic) ;
    btn1=   findViewById(R.id.Btn1);
    btn2=   findViewById(R.id.Btn2);
    btn3=   findViewById(R.id.Btn3);
    check=  findViewById(R.id.Check);

    final int array[]={
            R.drawable.one  ,
            R.drawable.two  ,
            R.drawable.three,
            R.drawable.four ,
            R.drawable.five ,
            R.drawable.six  , };

    final String picname[]={

            "One"  ,
            "Two"  ,
            "Three",
            "Four" ,
            "Five" ,
            "Six"  };

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

        //to randomly select the buttons and the images

            int i=(int)(Math.floor(array.length*Math.random()));
            int j=(int)(Math.floor(array.length*Math.random()));
            int k=(int)(Math.floor(array.length*Math.random()));

            pic.setImageResource(array[i]);
            btn1.setText(picname[i]);
            btn2.setText(picname[j]);
            btn3.setText(picname[k]);



        }



        });
    }




    }

//// below is a screenshot of the GUI

this is it working This is it working properly (what I want it to do "the answer is not repeated"




Aucun commentaire:

Enregistrer un commentaire