首页 > 解决方案 > 我想从循环中输出一次

问题描述

所以我有一个输出无限数组的代码,所以我想检查该数组的值是否低于 80。它按我的意愿打印出来,但它一直打印出来

这是我的代码:

package com.example.testtingskripsi;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Collections;
import java.util.Timer;

public class MainActivity extends AppCompatActivity {
    public TextView mViewLabel;
    boolean continueThread = true;
    int count =0;
    Thread t;


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

        mViewLabel = (TextView) findViewById(R.id.textChanger);

        ArrayList<Integer> lst = new ArrayList<Integer>();

        Timer j = new java.util.Timer();



        t = new Thread(){
            @Override
            public void run() {
                if (continueThread) {
                    while (continueThread) {
                        try {
                            Thread.sleep(1000);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    lst.add(70);
                                    lst.add(71);
                                    lst.add(72);
                                    lst.add(73);
                                    lst.add(74);
                                    lst.add(75);

                                    Collections.shuffle(lst);
                                    mViewLabel.setText(String.valueOf(lst.get(count)));

                                }

                            });
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        j.schedule(
                                new java.util.TimerTask() {
                                    @Override
                                    public void run() {
                                        if(lst.get(count) < 80){
                                            System.out.println("Ok");
                                        }
                                    }
                                },
                                5000
                        );
                        count++;
                    }
                }
            }
        };


    }
    public void BtnStart(View view){
        t.start();

    }
    public void BtnStop(View view){
        continueThread=false;
    }
}

如您所见,我知道它在循环中,但我想从数组中检查它是否不在循环中,它会出错。所以我只想打印出“Ok”这样任何人都可以帮我解决它吗?

标签: javaandroidandroid-studio

解决方案


尝试这个 :

t = new Thread(){
            @Override
            public void run() {
                if (continueThread) {
                    while (continueThread) {
                        lst.add(70);
                        lst.add(71);
                        lst.add(72);
                        lst.add(73);
                        lst.add(74);
                        lst.add(75);
                        try {
                            Thread.sleep(1000);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Collections.shuffle(lst);
                                    mViewLabel.setText(String.valueOf(lst.get(count)));
                                }

                            });

                        }catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        count++;

                    }

                }
            }
        };
        j.schedule(
                new java.util.TimerTask() {
                    @Override
                    public void run() {
                        while(continueThread){
                            if(lst.get(count) < 80){
                                System.out.println("Ok");
                                break;
                            }
                            count++;
                        }


                    }
                },
                5000
        );

推荐阅读