简体   繁体   中英

How to stop for loop running all at once

I am currently practising with for loops with array lists. Each time the user gets an answer correct an extra random value is added to the sequence. So at the start one image flashes, each time one more image flashes in sequence.

The problem I am have is that the whole sequence plays at once. For example on the second run two images should flash, one then the other. But what I currently get is both flash together. How can I break up the loop so one will flash and then the other.

my code is below:

ArrayList<Integer> list = new ArrayList<Integer>();
int retval;  

    public void Run(){

        Random rand = new Random();
        int rndInt = rand.nextInt(5)+ 1 ;       
        list.add(rndInt);
        int totalElements = list.size();

        for(int i=0; i < totalElements;i++ ){ 
            retval = list.get(i);

            if  (retval==1){

                Centre.setImageResource(R.drawable.flash1);
                Reset.start();// runs a class that resets the imageiew after a few seconds 
            }

            else if (retval==2){

                upperRight.setImageResource(R.drawable.flash2);
                Reset.start();
            }

            else if (retval==3){

                lowerRight.setImageResource(R.drawable.flash3);
                Reset.start();
            }

            else if (retval==4){    

                lowerLeft.setImageResource(R.drawable.flash4);
                Reset.start();
            }

            else if (retval==5){
                upperLeft.setImageResource(R.drawable.flash5);
                Reset.start();      
            }   
        }
    }

EDIT

CountDownTimer Reset = new CountDownTimer(1000 , 0010){ 
        public void onTick(long millisUntilFinished) {      
        }
        public void onFinish() {        
            Centre.setImageResource(R.drawable.i1);
            upperRight.setImageResource(R.drawable.i2);
            lowerRight.setImageResource(R.drawable.i3);
            lowerLeft.setImageResource(R.drawable.i4);
            upperLeft.setImageResource(R.drawable.i5);              
            }
        };

Ok so here is the fix incase anyone wants to show images in a sequence:

public void ZAP() { 
            if(i < totalElements){

                retval =list.get(i);

                if  (retval==1){                
                check = 1;              
                Centre.setImageResource(R.drawable.flash1);
             ResetTimer.start();        

            }       
            else if (retval==2){

                check = 2;          
                upperRight.setImageResource(R.drawable.flash2);                     
             ResetTimer.start();

            }   
            else if (retval==3){

                check =3;   
                lowerRight.setImageResource(R.drawable.flash3); 
                 ResetTimer.start();    

            }   
            else if (retval==4){

                check = 4;      
                lowerLeft.setImageResource(R.drawable.flash4); 
                 ResetTimer.start();

            }   
            else if (retval==5){

                check=5;
                upperLeft.setImageResource(R.drawable.flash5);  
                 ResetTimer.start();
            }}

            }
    public void Rand() {
            i=0 ;


        Random rand = new Random();
        int rndInt = rand.nextInt(5)+ 1 ;       
        list.add(rndInt);
         totalElements = list.size();
     Log.d("LOOK", Integer.toString(rndInt));
     Log.i("VALUE LIST ", list.toString()+" <<<<LIST HERE"); 
     ZAP();     

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM