簡體   English   中英

如何停止我的應用程序中的隨機數生成器而不凍結應用程序

[英]How do i stop a random number generator in my app without making the app to freeze

我的應用程序使用隨機數生成器向用戶顯示隨機問題,但是只有一個問題,該隨機數不會停止,並且始終會生成一個始終顯示不同問題的數字。

因此,我嘗試使用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次之后產生一個隨機問題,並顯示不同的布局,該布局將顯示已回答的問題總數

您的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
    }
}

祝好運 :)

總覽

是的,您的代碼存在一些格式問題,但是我在這里要大膽嘗試一下,並假設您最近才開始工作,因為您在這里遇到的問題非常明顯。

您的問題的簡單答案是:您不能。 您無法做的事。 您在此處創建的是一個無限循環!

這是正在發生的事情:

您創建一個while循環並告訴該循環:“只要變量'randomNumbers'的值大於0,請繼續重復”

在循環內部,每次while循環重復執行時,您將'randomNumbers'的值增加1。

這意味着您的while循環永遠不會停止重復! 幸運的是,這很容易解決。 我們需要做的就是更改將使while循環停止重復的內容。

當前while循環條件:

randomNumbers >= 0

這樣思考:“我什么時候希望while循環停止重復?我在數什么?”

這些問題的答案將構成您需要將while循環的條件更改為什么。 我相信,您需要的答案是將while循環條件更改為此:

while(randomNumbers is <= howManyQuestionsIWant)

那肯定會解決您的while循環重復問題。 僅快速瀏覽其余的代碼,那里還有其他錯誤,但這至少會使您擺脫重復的厄運問題循環,因此您可以開始調試其余部分。 祝好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM