首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >以10秒的间隔运行函数10次的最佳方法

以10秒的间隔运行函数10次的最佳方法
EN

Stack Overflow用户
提问于 2021-04-10 07:15:25
回答 1查看 41关注 0票数 0

基本上,我有一个为琐事游戏加载问题的函数。每场比赛有10轮。目前,我将它放在一个4循环中,并尝试使用CountDownTimer,但它仍然会遍历并立即调用该函数10次。只是在看一些关于如何预测持续10秒的回合然后进入下一轮的建议。

代码语言:javascript
复制
public class GameActivity extends AppCompatActivity {

    int questionCount = 0;
    int textUpdate = 0;

    Boolean readFlag = true;
    Button answerOneBtn, answerTwoBtn, answerThreeBtn, answerFourBtn;
    TextView questionTextView;

    int playerScore = 0;
    Game currentGame = new Game(questionCount, readFlag, playerScore);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);
        currentGame.questionList.AnswersJumbled();

        for(int i = 0; i < 10; i++) {
            Log.e("Error", "For Loop is Running");
            playGame();
        }

        answerOneBtn = findViewById(R.id.AnswerOneButton);
        answerTwoBtn = findViewById(R.id.AnswerTwoButton);
        answerThreeBtn = findViewById(R.id.AnswerThreeButton);
        answerFourBtn = findViewById(R.id.AnswerFourButton);
        questionTextView = findViewById(R.id.questionText);

        answerOneBtn.setOnClickListener(v -> {
            currentGame.playerOneSelection = answerOneBtn.getText().toString();
            Log.e("error", currentGame.playerOneSelection);
        });

        answerTwoBtn.setOnClickListener(v -> {
            currentGame.playerOneSelection = answerTwoBtn.getText().toString();
            Log.e("error", currentGame.playerOneSelection);
        });

        answerThreeBtn.setOnClickListener(v -> {
            currentGame.playerOneSelection = answerThreeBtn.getText().toString();
            Log.e("error", currentGame.playerOneSelection);
        });

        answerFourBtn.setOnClickListener(v -> {
            currentGame.playerOneSelection = answerFourBtn.getText().toString();
            Log.e("error", currentGame.playerOneSelection);
        });
    }

    public void playGame() {
        answerOneBtn = findViewById(R.id.AnswerOneButton);
        answerTwoBtn = findViewById(R.id.AnswerTwoButton);
        answerThreeBtn = findViewById(R.id.AnswerThreeButton);
        answerFourBtn = findViewById(R.id.AnswerFourButton);
        questionTextView = findViewById(R.id.questionText);

        currentGame.loadQuestion(questionCount);
        questionTextView.setText(currentGame.currentQuestion);
        answerOneBtn.setText(currentGame.firstAnswer);
        answerTwoBtn.setText(currentGame.secondAnswer);
        answerThreeBtn.setText(currentGame.thirdAnswer);
        answerFourBtn.setText(currentGame.fourthAnswer);

        questionCount++;
    }
}
EN

回答 1

Stack Overflow用户

发布于 2021-04-11 05:05:16

好的,经过一些挖掘,并被评论指向大致的方向,我发现正确的答案是处理器。

我的for循环现在看起来像这样

代码语言:javascript
复制
for (int i = 0; i < 10; i++) {

        handler.postDelayed(() -> playGame(), 10000 * i);
        playerAnswered = false;
        playerScore += currentGame.checkPlayerAnswer(currentGame.playerOneSelection);

    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67029353

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档