How to randomize if statements within android deve

2019-09-18 19:24发布

问题:

I am building a questionnaire type application within android and I am wondering if there is a way to randomize the final output if the if statements have the same values? In other terms I don't want one single result that occurs every time, I want the result to be randomized between 'GOOD JOB' and 'WELL DONE' does anybody know if this is possible?

Here is my Code:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_system);
    RadioGroup gender = (RadioGroup) findViewById(R.id.answer1);
    gender.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // TODO Auto-generated method stub
            switch (checkedId) {
            case R.id.answer1A:
                ans1 = 1;
                break;
            case R.id.answer1B:
                ans1 = 2;
                break;
            }
        }
    });
    RadioGroup nutrition = (RadioGroup) findViewById(R.id.answer2);
    nutrition.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // TODO Auto-generated method stub
            switch (checkedId) {
            case R.id.answer2A:
                ans2 = 1;
                break;
            case R.id.answer2B:
                ans2 = 2;
                break;
            }
        }
    });


    btnSubmitQuiz = (Button) findViewById(R.id.submit);
    btnSubmitQuiz.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // 1
            if (ans1 == 1 && ans2 == 1) {

                displayResult("good job");

            }

            if (ans1 == 1 && ans2 == 1 ) {

                displayResult("well done");

            }


            else {

                displayResult("FAIL");

            }
        }

        private void displayResult(String result) {
            Intent i = new Intent("com.example.system.SHOWRESULT");
            i.putExtra("unique_constant", result);
            startActivity(i);

        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.system, menu);
    return true;
}

}

回答1:

You could have a function to generate a random index from a string array of correct result responses.

String[] correctResults;
Random randomGenerator;

public void onCreate (Bundle savedInstanceState) {
    //All your init stuff...
    generator = new Random();
    correctResults = new String[] {"good job", "well done"};
}

public String getRandomCorrectResult() {
    return correctResults[generator.nextInt(correctResults.length)];
}

And just call getRandomCorrectResult() inside of that if statement!



回答2:

I'm not going to write actual code for you because that takes the fun away, but instead of displayResult("GOOD JOB") you could have a function called displayPositiveResult()

In this function you would randomly select a string from an array of strings containing things like "Great job!", "You did it!", "Way to go!"

You get the idea.

Hope this helps!