简体   繁体   中英

How do i fix this loop in a loop in a array, Java Eclipse?

  1. This code is for a eclipse learning class and our teacher is out can someone explain the error please and thank you. We are trying to create a loop within a loop inside an array.

package Wrok;

import java.util.Random;

public class Victory {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Random ran = new Random(); 
        double x = ran.nextInt(6) + 5;
        Random ran1 = new Random(); 
        double y = ran.nextInt(6) + 5;
        int Time = -1;
        double[][] hello = new double [2][2];
                hello[0][0]= 1;
                hello[0][1]= 2;
                hello[1][0]= 3;
                hello[1][1]= 4;
        for (int i = 0; i<Time; i++){
            for (int j = 0; j<Time; j++){
                if(ran = 0){
                    if (ran1 = 0){
                        System.out.println(hello[0][0]);
                    }
                }
                if(ran = 1){
                    if (ran1 = 0){
                        System.out.println(hello[1][0]);
                    }
                }
                if(ran = 0){
                    if (ran1 = 1){
                        System.out.println(hello[0][1]);
                    }
                }
                if(ran = 1){
                    if (ran1 = 1){
                        System.out.println(hello[1][1]);
                    }
                }
            }
        }
    }
}

The loops run from i to Time . However, i = 0 and int Time = -1 therefore the loops will never be entered as '0 < -1 yields false.

Furthermore, if(ran = 0) this is incorrect, ran is a an object from the Random class you instantiated to get random numbers. The random numbers were saved to variables x and y.

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