80.9k views
0 votes
Create a method called randomValues that uses a while loop to generate a random number between 1-25 until the value 10 is generated. Therefore, 10 will be the value that stops your loop from generating a random number.

1 Answer

1 vote

Answer:

The method in Java is as follows:

public static void randomValues(){

Random r = new Random();

int sentinel = 10;

int randNum = r.nextInt(24) + 1;

while(randNum!=sentinel){

System.out.print(randNum+" ");

randNum = r.nextInt(24) + 1;

}

System.out.print(sentinel);

}

Step-by-step explanation:

This defines the method

public static void randomValues(){

This creates a random object, r

Random r = new Random();

This sets the sentinel value to 0

int sentinel = 10;

This generates a random number

int randNum = r.nextInt(24) + 1;

This loop is repeated until the the random number is 10

while(randNum!=sentinel){

Print the generated number

System.out.print(randNum+" ");

Generated another random number

randNum = r.nextInt(24) + 1; }

Print the sentinel (i.e. 10)

System.out.print(sentinel);

}

User Kirlyn
by
3.6k points