vendredi 17 février 2017

How do I create a random integer that changes with each x value in a for loop in Java?

I have the for loop created but I would like the randNumber and zNumber to change as the x value increases in the for loop, so that the output (system.out.println) is different every time. How do I do this? Also, is there a way to make it so that if the output of the randNumber is lower than say, 75, the output of the randNumber will change to a lower number than 85? Like if the random number generator chooses 75 as the first number is there a way to change the value of the rest of the outputs after that to be lower than 85 (what it is set to now).

public static void Hypothetical() {
    int x;
    Random z = new Random();
    int zNumber = z.nextInt(10-5) + 5;
    Random rand = new Random();
    int randNumber = rand.nextInt(85-65) + 65;
    Random
    for (x = 0; x <= 4; x++) {
        if (x  == 0) {
            System.out.println("You may begin running.");
            //TimeUnit.SECONDS.(2);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        if (x == 1 ) {
            System.out.println("Your first lap was made in " + randNumber + " seconds");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        if (x==2) {
            System.out.println("Your second lap was made in " + (randNumber + zNumber) + " seconds");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        if (x==3) {
            System.out.println("Your third lap was made in " + (randNumber + zNumber) + " seconds");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        if (x==4) {
            System.out.println("Your final lap was made in " + (randNumber + zNumber) + " seconds");
        }
    }
}

}




Aucun commentaire:

Enregistrer un commentaire