如何停止应用中的随机数生成器而不冻结应用

时间:2019-02-01 17:08:43

标签: java android

我的应用程序使用随机数生成器向用户显示随机问题,但是只有一个问题,这个随机数不会停止,并且会始终生成一个始终显示不同问题的数字。

因此,我尝试使用while和if循环停止该操作,在写完该代码后,我注意到该应用程序保持冻结状态。

在不冻结应用程序的情况下停止此随机数生成器的最佳方法是什么。

package com.myafrica.africaquiz;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;
import java.util.Random;


public class MainActivity extends AppCompatActivity {

//this variable stores the total number of correct answers
int StoresTotalCorrectAnswer;

//this variable count the total number of RandomQuestions asked
int RandomNumberCounter;

//this variable stores the random numbers generated
int RandomNumber;

//variables of the views for public access to other classes
TextView TestQuestionID = null;
RadioButton AnswerA_ID = null;
RadioButton AnswerB_ID = null;
RadioButton AnswerC_ID = null;
RadioButton AnswerD_ID = null;

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

    //get the text view and stores it in this variable
    TestQuestionID = findViewById(R.id.TestQuestion);

    //get the radio button view and stores it in this variable
    AnswerA_ID = findViewById(R.id.AnswerA);
    AnswerB_ID = findViewById(R.id.AnswerB);
    AnswerC_ID = findViewById(R.id.AnswerC);
    AnswerD_ID = findViewById(R.id.AnswerD);
}

//When the onclicked() is clicked this method runs
public void GetNextQuestion(View view) {
    RandomNumberGenerator();
}

//Creating a random number generator
public void RandomNumberGenerator() {

    Random RanNum = new Random();
    RandomNumber = RanNum.nextInt(4) + 1;

    //to know how many times the random method has been called
    while (RandomNumber >= 0) {
        RandomNumberCounter++;

        //to stop the questions from displaying once the required number is 
 //reached
        if (RandomNumberCounter < 4) {

            switch (RandomNumber) {

                case 1:
                    Question1();
                    break;

                case 2:
                    Question2();
                    break;

                case 3:
                    Question3();
                    break;

                case 4:
                    Question4();
                    break;
            }

            // not done with this part yet, but was trying to store the 
 //correct answers
            if (RandomNumber == 1 && AnswerA_ID.isChecked()) {
                StoresTotalCorrectAnswer++;
            } else if (RandomNumber == 2 && AnswerA_ID.isChecked()) {
                StoresTotalCorrectAnswer++;
            } else if (RandomNumber == 3 && AnswerD_ID.isChecked()) {
                StoresTotalCorrectAnswer++;
            } else if (RandomNumber == 4 && AnswerA_ID.isChecked()) {
                StoresTotalCorrectAnswer++;
            }
        }

        // shows a different layout after the question stops showing
        else {
            Intent i = new Intent(this, Main2Activity.class);
            startActivity(i);
        }
    }
}


 /*
    This part of the code will contain the answer part of this quiz app.
    Each question is a method and that method will be called when the Next 
button is clicked in the app.
    When the button is clicked the setText() will automatically (re)set the 
text in the question layout in the XML
     */
public void Question1() {
    TestQuestionID.setText("Which part of Africa can Ghana be located");
    AnswerA_ID.setText(" A: West Africa");
    AnswerB_ID.setText(" B: East Africa");
    AnswerC_ID.setText(" C: Central Africa");
    AnswerD_ID.setText(" D: North Africa");
}

public void Question2() {
    TestQuestionID.setText("What is the capital of Nigeria");
    AnswerA_ID.setText(" A: Abuja");
    AnswerB_ID.setText(" B: Kano");
    AnswerC_ID.setText(" C: Lagos");
    AnswerD_ID.setText(" D: Port Harourt");
}

public void Question3() {
    TestQuestionID.setText("Which of these countries are not located in 
Africa");
    AnswerA_ID.setText(" A: Niger");
    AnswerB_ID.setText(" B: Nigeria");
    AnswerC_ID.setText(" C: Rwanda");
    AnswerD_ID.setText(" D: Non of the Above");
}

public void Question4() {
    TestQuestionID.setText("What is the estimated population of Africa, as at 
 2015");
    AnswerA_ID.setText(" A: 1.2 Billion");
    AnswerB_ID.setText(" B: 500 Billion");
    AnswerC_ID.setText(" C: 2 Billion");
    AnswerD_ID.setText(" D: 360 million");
}
}

我厌倦了使对代码的注释易于理解,因此任何人都可以理解代码的每个部分将做什么。

注意:我真正需要的是一种停止随机数生成器的方法,该生成器总是在X次之后产生一个随机问题,并显示不同的布局,该布局将显示已回答的问题总数

2 个答案:

答案 0 :(得分:0)

while循环没有达到退出条件,这就是为什么它正在做的工作下去。当您达到理想状态时,应该做的是break。例如,在您的else语句中,从新的Activity开始,您应该将break;放在末尾:

...    

int RandomNumberCounter = 0;     // you have to assign a sum variable to be 0 or else it stores random value.

...

while (RandomNumber >= 0) {
    RandomNumberCounter++;

    if (RandomNumberCounter < 4) {
        ...
    }
    else {   
        Intent i = new Intent(this, Main2Activity.class);
        startActivity(i);
        break;               // this breaks execution of your while loop
    }
}

祝你好运:)

答案 1 :(得分:0)

概述

是的,您的代码存在一些格式方面的问题,但是我将在这里进行一番尝试,并假设您是最近开始的,因为您在这里遇到的问题非常明显。

您的问题的简单答案是:不能。您无法做的事。您在这里创建的是一个无限循环!

这是正在发生的事情:

您创建一个while循环并告诉该循环:“只要变量'randomNumbers'的值大于0,请继续重复”

在循环内部,每次while循环重复进行时,您都会将“ randomNumbers”的值增加1。

这意味着您的while循环永远不会停止重复! 幸运的是,这很容易解决。我们需要做的就是更改将使while循环停止重复的内容。

当前while循环条件:

randomNumbers >= 0

这样想:“什么时候我的while循环停止重复?我在计数什么?”

这些问题的答案将构成您需要将while循环的条件更改为什么。我相信,您需要的答案是将while循环条件更改为此:

while(randomNumbers is <= howManyQuestionsIWant)

那肯定会解决您的while循环重复问题。仅快速浏览其余的代码,那里还有其他错误,但这至少会使您摆脱重复的厄运问题循环,因此您可以开始调试其余部分。祝你好运!