mercredi 13 décembre 2017

Java Using the return value of a loop within the loop

sorry but english is nut my first language. I'm building a main class and method to create a bunch of random numbers. I want to use a for-loop and want to use with every step of the loop the result of the last loop.

The User is asked to enter 3 integers to create a random number with the formula: z = (a*z+b)%m;

1.Step z = (a*0+b)%m; Lets say the result is 7. 2. Step: z= (a*7+b)%m and so on.

I want the program to give me 20 random numbers. In the first step of the loop the random number z is 0, and then with every additional step the random number is built from the result of the last step. I hope you guys understand what I'm trying to say.

My Code so far:

import java.io.*; public class Zufallszahlentest {

public static void main (String [] args)throws IOException {

    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Geben Sie nacheinander die Werte für a,b &m ein.");
    int a = Integer.parseInt(in.readLine());
    int b = Integer.parseInt(in.readLine());
    int m = Integer.parseInt(in.readLine());

    Methode(a,b,m);
    System.out.println(Methode(a,b,m));


}

 static int Methode(int a, int b, int m){

     int z= 0;
        for (int i = 0; i <= 20; i++) {
             z =  (a*z+b)%m;

     }
        return z;

 } }

I've been sitting on this for hours, I've built huge loops with no result and I feel like getting dumber and dumber with every try.

Help would be highly appreciated.




Aucun commentaire:

Enregistrer un commentaire